Delete pages from a PDF file in Python
Last Updated :
28 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
# Path of the PDF file
input_file = r"test.pdf"
# Path for the output PDF file
output_file = r"modified_test.pdf"
# Opening the PDF file and creating a handle for it
file_handle = fitz.open(input_file)
# The page no. denoted by the variable will be deleted
page = 0
# Passing the variable as an argument
file_handle.delete_page(page)
# Saving the file
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 createdMethod 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
# Path of the PDF file
input_file = r"test.pdf"
# Path for the output PDF file
output_file = r"modified_test.pdf"
# Opening the PDF file and creating a handle for it
file_handle = fitz.open(input_file)
# The index (page no.) from where the pages are to be deleted
start = 2
# The index to which the pages are to be deleted
end = 7
# Passing the start & end index as arguments
file_handle.delete_pages(start, end)
# Saving the file
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
# Path of the PDF file
input_file = r"test.pdf"
# Path for the output PDF file
output_file = r"modified_test.pdf"
# Opening the PDF file and creating a handle for it
file_handle = fitz.open(input_file)
# This list contains the pages that we are willing to keep
# Rest are deleted
pages_list = [0,1,3]
# Passing the list to the select function
file_handle.select(pages_list)
# Saving the file
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 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 Pyt
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
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
Check if a string exists in a PDF file in Python In this article, we'll learn how to use Python to determine whether a string is present in a PDF file. In Python, strings are essential for Projects, applications software, etc. Most of the time, we have to determine whether a string is present in a PDF file or not. Here, we'll discuss how to check
2 min read
How to extract images from PDF in Python? The task in this article is to extract images from PDFs and convert them to Image to PDF and PDF to Image in Python.To extract the images from PDF files and save them, we use the PyMuPDF library. First, we would have to install the PyMuPDF library using Pillow.pip install PyMuPDF PillowPyMuPDF is us
3 min read
How to Extract Data from PDF file in Android? PDF is a portable document format that is used to represent data such as images, tables, and many more. Nowadays the use of PDF is increased rapidly in different fields. Many apps have switched overusing PDF files to represent data. So some of the apps have a requirement to extract the data from the
4 min read