Rotate pdf programmatically using python

python pypdf

Recently I stubmled upon an old scanned copy of a book (out of print now) but the scanner had scanned the alternate pages inverted. Preview on mac has the capability to roate individual pages but no such option exists for bulk rotation.

Pypdf is a python library to manipulate pdf’s. Using pypdf I was able to rotate alternate pages.

We can install pypdf either using pip pip install pypdf or conda conda install -c conda-forge pypdf. Afterwards, its just reading the file, rotating alternate pages and writing it afterwards.

from pypdf import PdfReader, PdfWriter

# read file
reader = PdfReader("input.pdf")
writer = PdfWriter()

# rotate alternate pages.
for page_num in range(len(reader.pages)) :
    page = reader.pages[page_num]
    if page_num % 2 : 
    else :

# write file          
with open("output.pdf", "wb") as fp: