How to rotate PDF pages using Python program language?

VeryUtils PythonPDF Library is a pure Python library to manipulate PDFs from Python. You can use it to rotate some pages in your PDF files. For example, if you scan a few pages from a book to PDF file, turning the book around every other page, so half the pages in the PDF are upside down. Now, you can use PythonPDF library to write a python script to rotate the even numbered pages. The python script counts pages from 0, so it rotates the odd numbered pages from its perspective.

VeryUtils PythonPDF Library can be purchased from this web page,

After you buy it, you will get a download URL to its source code right now, please download that source code package and unzip it to a folder, such as,


you need also set “PYTHONPATH” to the main folder PythonPDF Library, for example,

set PYTHONPATH=D:\downloads\python-pdfrw

after you set the path to “PYTHONPATH“, please run following command line to rotate the PDF pages in your PDF file using PythonPDF Library,

python testcmd.pdf 270 1-3 5 7-9

usage: my.pdf rotation [page[range] ...]
         eg. 270 1-3 5 7-9

        Rotation must be multiple of 90 degrees, clockwise.

Creates with selected pages rotated. Rotates all by default.

Here is the screenshot of rotated PDF file,


This is the source code of file,
import sys
import os

from pdfrw import PdfReader, PdfWriter

inpfn = sys.argv[1]
rotate = sys.argv[2]
ranges = sys.argv[3:]

rotate = int(rotate)
assert rotate % 90 == 0

ranges = [[int(y) for y in x.split('-')] for x in ranges]
outfn = 'rotate.%s' % os.path.basename(inpfn)
trailer = PdfReader(inpfn)
pages = trailer.pages

if not ranges:
    ranges = [[1, len(pages)]]

for onerange in ranges:
    onerange = (onerange + onerange[-1:])[:2]
    for pagenum in range(onerange[0]-1, onerange[1]):
        pages[pagenum].Rotate = (int(pages[pagenum].inheritable.Rotate or
                                     0) + rotate) % 360

outdata = PdfWriter(outfn)
outdata.trailer = trailer
If you have any question for PythonPDF Library, please feel free to let us know, we are glad to assist you asap.

No votes yet.
Please wait...

Related Posts

Leave a Reply

Your email address will not be published.