Last active
January 15, 2020 17:34
-
-
Save alekseypotapov-dev/897c18ff5166b6f0b33b274afbd6894e to your computer and use it in GitHub Desktop.
Rotate all PDFs in a folder and put into another one
This file contains hidden or 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 | |
import re | |
import sys | |
import os | |
import shutil | |
dir_path = os.getcwd() | |
rotatedDir_path = dir_path + '/rotated' | |
if not os.path.exists(rotatedDir_path): | |
os.makedirs(rotatedDir_path) | |
directory = os.fsencode(dir_path) | |
for file in os.listdir(directory): | |
filename = os.fsdecode(file) | |
if filename.endswith(".pdf"): | |
pdfIn = open(filename, 'rb') | |
pdfReader = PyPDF2.PdfFileReader(pdfIn) | |
pdfWriter = PyPDF2.PdfFileWriter() | |
for pageNum in range(pdfReader.numPages): | |
page = pdfReader.getPage(pageNum) | |
page.rotateClockwise(90) # rotate by 90 degrees | |
pdfWriter.addPage(page) | |
pdfOut = open(rotatedDir_path + '/' + filename, 'wb') | |
pdfWriter.write(pdfOut) | |
pdfOut.close() | |
pdfIn.close() | |
continue | |
else: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rotate all pages in all files in directory