Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Delete all the Png Images from a Folder in Python
Next article icon

Delete pages from a PDF file in Python

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Delete pages from a PDF file in Python
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  # 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.

 

Next Article
Delete all the Png Images from a Folder in Python

V

vasudev4
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Technical Scripter 2022
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences