Vivek Gani
Vivek Gani

Reputation: 1313

How can I remove every other page of a pdf with python?

I downloaded a pdf where every other page is blank and I'd like to remove the blank pages. I could do this manually in a pdf tool (Adobe Acrobat, Preview.app, PDFPen, etc.) but since it's several hundred pages I'd like to do something more automated. Is there a way to do this in python?

Upvotes: 2

Views: 1419

Answers (1)

Vivek Gani
Vivek Gani

Reputation: 1313

One way is to use pypdf, so in your terminal first do

pip install pypdf4

Then create a .py script file similar to this:

# pdf_strip_every_other_page.py

from PyPDF4 import PdfFileReader, PdfFileWriter

number_of_pages = 500
output_writer = PdfFileWriter()
with open("/path/to/original.pdf", "rb") as inputfile:
    pdfOne = PdfFileReader(inputfile)

    for i in list(range(0, number_of_pages)):
        if i % 2 == 0:
            page = pdfOne.getPage(i)
            output_writer.addPage(page)

    with open("/path/to/output.pdf", "wb") as outfile:
        output_writer.write(outfile)

Note: you'll need to change the paths to what's appropriate for your scenario.

Obviously this script is rather crude and could be improved, but wanted to share it for anyone else wanting a quick way to deal with this scenario.

Upvotes: 5

Related Questions