Delete pages from a PDF file in Python
Last Updated :
26 Apr, 2025
In this article, We are going to learn how to delete pages from a pdf file in Python programming language.
Introduction
Modifying documents is a common task performed by many users. We can perform this task easily with Python libraries/modules that allow the language to process almost any file, the possibility of data processing inside Programming languages have become limitless. This article is about how to delete pages from a PDF file in Python.
Prerequisite:
The PyMuPDF library will be used for PDF processing in this article. To install the library in our system, run the following command in the command prompt.
pip install pymupdf
NOTE: This library is imported by using the following command.
import fitz
Deleting Pages with PyMuPDF
The PyMuPDF library offers various methods that simplify deleting pages from a PDF file. It allows specifying a single page, a range of page numbers, or a list with the page numbers.
Using each method, the following examples demonstrate how to delete pages from PDF files.
Input pdf file used:
Method 1: Deleting a singular page from a PDF
The delete_page() function in the library allows the deletion of a single page. The function takes an argument of the page number. The page associated with the number is deleted in the PDF. Here also indexing starts from ‘0’ so if we pass ‘0’ as an argument first page will be deleted. The following example deletes page number 1.
Note: The pdf file and program should in the same folder to avoid an error because we are not passing the path.
Python3
import fitz
input_file = r "test.pdf"
output_file = r "modified_test.pdf"
file_handle = fitz. open (input_file)
page = 0
file_handle.delete_page(page)
file_handle.save(output_file)
|
Output: After running the above code a new file is generated with the name ‘modified_test.pdf’ in which first page is deleted.

modified_test.pdf file created
Method 2: Deleting a range of page numbers from a PDF
The delete_pages() method in the Python library allows for the deletion of a range of page numbers. The function considers two variables: first, the starting index, and second, the ending index. The pages between these indexes will be deleted. The following example opens the PDF file and deletes the pages between 2 and 7 page numbers.
Python3
import fitz
input_file = r "test.pdf"
output_file = r "modified_test.pdf"
file_handle = fitz. open (input_file)
start = 2
end = 7
file_handle.delete_pages(start, end)
file_handle.save(output_file)
|
Output: After running the above code we get the modified pdf file in which pages number 3, 4, 5, 6, 7, and 8 are deleted.
Method 3: Deleting a list of pages from a PDF
Similarly, the select() method allows the deletion of pages based on their numbers. i.e., The select function takes a list as an argument containing the page number of the pages we are willing to preserve, and the rest of the pages are deleted. Ex. If a PDF contains 10 pages, and we pass in argument the list [1, 3, 5] to the select function, then only these pages will remain, and the rest will be deleted. The following example deletes all the pages other than the page numbers 0, 1, and 3 from the PDF.
Python3
import fitz
input_file = r "test.pdf"
output_file = r "modified_test.pdf"
file_handle = fitz. open (input_file)
pages_list = [ 0 , 1 , 3 ]
file_handle.select(pages_list)
file_handle.save(output_file)
|
Output: The output of the above code is a modified pdf file in which only pages 1, 2, and 4 are present rest are deleted.

Similar Reads
How to delete from a pickle file in Python?
Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. What pickle does is that it âserializesâ the object first before writing it to file. Pickling is a way to convert a python object (list, dic
3 min read
How to delete data from file in Python
When data is no longer needed, itâs important to free up space for more relevant information. Python's file handling capabilities allow us to manage files easily, whether it's deleting entire files, clearing contents or removing specific data. For more on file handling, check out: File Handling in P
3 min read
Delete all the Png Images from a Folder in Python
Python is mostly used to automate tasks, including file management operations. Deleting all PNG images from a folder can be efficiently handled using Python. In this article, we will explore two different approaches to Deleting all the PNG images from a Folder in Python. Delete all the PNG images fr
2 min read
Read a Particular Page from a PDF File in Python
Document processing is one of the most common use cases for the Python programming language. This allows the language to process many files, such as database files, multimedia files and encrypted files, to name a few. This article will teach you how to read a particular page from a PDF (Portable Doc
4 min read
How to delete a CSV file in Python?
In this article, we are going to delete a CSV file in Python. CSV (Comma-separated values file) is the most commonly used file format to handle tabular data. The data values are separated by, (comma). The first line gives the names of the columns and after the next line the values of each column. Ap
2 min read
How to Delete Pages in Excel
In Excel, the concept of "pages" typically refers to worksheets, print areas, or extra blank spaces that may clutter your file. Whether youâre looking to clean up your workbook, remove unwanted sheets, or delete unnecessary print areas, knowing how to delete pages in Excel is essential for maintaini
3 min read
Delete files older than N days in Python
In this article, we will learn to delete files that are older than a given number of days using Python. This work can be done manually but it is difficult to do when files are more in number, so we will use Python to make this task simple. We will learn three ways to do this task using Python: Delet
5 min read
Cut a MP3 file in Python
An MP3 file is an audio file that uses a compression algorithm to reduce the overall file size. In this article, we will learn how to cut a particular portion of an MP3 file in Python. Here we will first open the mp3 audio then slice the audio with Python code and save the result. Before we go forw
3 min read
How to count the number of pages in a PDF file in Python
In this article, we will see how can we count the total number of pages in a PDF file in Python, For this article there is no such prerequisite, we will use PyPDF2 library for this purpose. PyPDF2 is a free and open-source pure-Python PyPDF library capable of performing many tasks like splitting, me
4 min read
How to Delete a Page in Word
Unwanted pages in a Word document can affect the overall appearance and structure of your content. Whether you're dealing with a blank page at the end of your file, an extra page in the middle, or working on a Mac and unsure how to remove it, knowing how to delete a page in Word is an essential skil
6 min read