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,

https://veryutils.com/pythonpdf-library-source-code

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,

D:\downloads\python-pdfrw

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 rotate.py testcmd.pdf 270 1-3 5 7-9

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

        Rotation must be multiple of 90 degrees, clockwise.

Creates rotate.my.pdf with selected pages rotated. Rotates all by default.

Here is the screenshot of rotated PDF file,

image

This is the source code of rotate.py 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
outdata.write()
---------------------------------------------------------
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.