Python Program to Delete Specific Pages in a PDF
A Python utility that removes selected pages from a PDF using PyPDF2. Users can specify one or multiple page numbers to delete, and the script generates a clean PDF without the unwanted pages.
Explanation
A PDF Page Deleter is a Python utility program that removes selected pages from an existing PDF file. This is useful when you want to remove blank pages, unwanted advertisement pages, wrong pages, duplicated pages, or any specific pages that are unnecessary in the final document.
This program uses PyPDF2 to read the PDF, take a list of page numbers
from the user, skip those pages, and finally generate a new PDF file without the
unwanted pages. The user can delete one page, multiple pages, or any random
set of pages using comma-separated values.
Requirements
-
pip install PyPDF2
Installs the PyPDF2 library required for reading and modifying PDF files.
- Python version 3 or higher.
- A PDF file with pages you want to delete.
- Page numbers must be entered separated by commas (example:
1,3,5). - Generates a new cleaned PDF file without the removed pages.
Code Explanation
Imports the required PyPDF2 classes for reading and writing PDF pages.
Prints a title formatted with = for clean console display.
Asks the user to enter the PDF file path that needs modification.
Takes user input of pages to delete and splits them into a list.
Example: 1,4,6 becomes ["1","4","6"].
Gets the name for the final PDF after deleting the requested pages.
total_pages = len(reader.pages)
Reads the input PDF and counts the total number of pages.
Creates a new PdfWriter object to store pages that should be kept.
if(str(i+1) not in deleted_pages):
writer.add_page(reader.pages[i])
Loops through each page: ✔ If the page number is NOT in delete list → add to output ✔ If it IS in delete list → skip it
writer.write(f)
Saves the cleaned PDF containing only the pages that were not deleted.
Key Points
- Deletes one or multiple pages from any PDF file.
- User enters page numbers as comma-separated values.
- Efficient and fast using PyPDF2.
- Creates a new PDF file without modifying the original.
- Works for PDFs of any length and size.
Full Python Program
from PyPDF2 import PdfWriter, PdfReader
print(" Delete PDF Pages ".center(40,"="))
input_pdf = input("Enter The PDF File Path : ")
deleted_pages = input("Enter The Page Number You Want To Delete using , (1,2,5) : ").split(",")
output_pdf = input("Enter The Output PDF File Name : ")
reader = PdfReader(input_pdf)
total_pages = len(reader.pages)
writer = PdfWriter()
for i in range(total_pages):
if(str(i+1) not in deleted_pages):
writer.add_page(reader.pages[i])
with open(output_pdf, 'wb') as f:
writer.write(f)
Output :
> py deletePdfPages.py =========== Delete PDF Pages =========== Enter The PDF File Path : sample.pdf Enter The Page Number You Want To Delete using , (1,2,5) : 2,4 Enter The Output PDF File Name : newFile.pdf Pages deleted successfully (kept all except 2 & 4) Output saved as: newFile.pdf
