Python Script: delete pages from a PDF file (Source code download)


Deleting pages from a PDF can be necessary for a number of reasons. Maybe you scanned a document and it turned out there were some blank pages mixed in. Maybe you have a PDF that’s just too long and you want to get rid of some of the content. Whatever the reason, it’s actually quite simple to delete pages from a PDF using Python.

To delete pages from a PDF file using pdfrw package of Python

pdfrw is a pure Python library that reads and writes PDFs. PyPDF2 supports more PDF features than pdfrw, like decryption and more types of decompression. It also lets you do things like combine the bookmarks from two different PDF files. In this post, I use pdfrw library to delete pages from a PDF file.

The first step is to install the pdfrw package. This package will allow us to read/write PDFs and make all sorts of edits. To do this, open your command line and type “pip install pdfrw” (without the quotes).

$ pip install pdfrw

Once that’s finished downloading, open your Python IDE and import the package like so:

from pdfrw import PdfWriter, PdfReader
Code language: JavaScript (javascript)

Now we need to open the PDF we want to edit. In order to do this, we use the PdfReader class from pdfrw. We need to give our file path as an argument like so:

pdf_object = PdfReader(file)

At this point, our file is loaded into memory and ready to be edited. If we want to see how many pages are in the document, we can use the len(pdf_object.pages) function like so:

total_pages = len(pdf_object.pages) print(total_pages) # Output: 5
Code language: PHP (php)

From here, we need to create a writer object. The writer object is what we’ll use to make our edits and write them back out as a new PDF. We do this with the PdfWriter class like so:

writer = PdfWriter()


To delete pages, we add only those pages that we need to a list excluding del_pages.

del_pages is a list of the pages to be deleted.

# Adding only those pages that we need to this list excluding del_pages page_list = [ page for page in range(1, total_pages + 1) if page not in del_pages ] # Index of pdf_object.pages starts from 0. for page in page_list: writer.addpage(pdf_object.pages[page - 1])
Code language: PHP (php)

We can then save our newly edited PDF like so:

writer.write(path)
Code language: CSS (css)

That’s it! You now know how to delete pages from a PDF in Python using pdfrw library.

A full Python Script to delete pages from a PDF file

from pdfrw import PdfWriter, PdfReader import os def delete(path, del_pages): """ The function delete takes two arguments path and del_page path is the path of the source pdf file. This function deletes the pages from the pdf file. Parameters: path : Path of the pdf file. del_pages : A list of pages to be deleted. Returns: None """ # create a pdf object using PdfReader that could be read pdf_object = PdfReader(path) # pdf_object.pages attribute gives the length of the pages in pdf total_pages = len(pdf_object.pages) print("Total Pages in PDF are:", total_pages) # Initialising the writer object using the PdfWriter class writer = PdfWriter() # Adding only those pages that we need to this list excluding del_pages page_list = [ page for page in range(1, total_pages + 1) if page not in del_pages ] # Index of pdf_object.pages starts from 0. for page in page_list: writer.addpage(pdf_object[page - 1]) # removing the original pdf os.remove(path) # writing the modified file to the memory writer.write(path) path = input("enter the path(full or relative) of the pdf file:") del_pages = input( "Enter the pages to be deleted seperated by comma(,):").strip().split(",") del_pages = [int(i) for i in del_pages] delete(path, del_pages) print("\n\t pages", *del_pages, "have been deleted successfully!!!")
Code language: Python (python)

Download Source code

Conclusion:

Given a few simple steps, you can easily remove individual pages from any PDF file using Python and pdfrw ! Whether your document has extra blank pages or you just need to shorten it up, deleting pages is a quick and easy way to get your PDF looking just right.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts