Last active
August 29, 2024 13:37
-
-
Save jb0hn/760447d7737555793efe48fb4192802c to your computer and use it in GitHub Desktop.
Rotate PDF file with PyPDF2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import PyPDF2 | |
pdfIn = open('original.pdf', 'rb') # exchange the 'original.pdf' with a name of your file | |
pdfReader = PyPDF2.PdfFileReader(pdfIn) | |
pdfWriter = PyPDF2.PdfFileWriter() | |
for pageNum in range(pdfReader.numPages): | |
page = pdfReader.getPage(pageNum) | |
page.rotateClockwise(90) | |
pdfWriter.addPage(page) | |
pdfOut = open('rotated.pdf', 'wb') | |
pdfWriter.write(pdfOut) | |
pdfOut.close() | |
pdfIn.close() |
fname = 'original.pdf' # exchange the 'original.pdf' with a name of your file
new_fname = fname.replace('.pdf', '_rotated.pdf')
pdfIn = open(fname, 'rb')
pdfReader = PyPDF2.PdfReader(pdfIn)
pdfWriter = PyPDF2.PdfWriter()
for page in pdfReader.pages:
page.rotate(90)
pdfWriter.add_page(page)
pdfOut = open(new_fname, 'wb')
pdfWriter.write(pdfOut)
pdfOut.close()
pdfIn.close()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jb0hn Really helpful!!
Some of the code written above are deprecated functions since PyPDF2 version 3.0.0.
Here is the same code but adapted: