Skip to content

Instantly share code, notes, and snippets.

@deadjakk
Created October 3, 2023 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deadjakk/c1da8e6207b48432175b3bd0be63d966 to your computer and use it in GitHub Desktop.
Save deadjakk/c1da8e6207b48432175b3bd0be63d966 to your computer and use it in GitHub Desktop.
Rotates provided pages of a file named input.pdf and saves it to output.pdf
import PyPDF2
import sys
rotatepages = []
inps = sys.argv[1:]
if len(inps) < 1:
print("rotates pages of any pdf named inp.pdf, saves it to output.pdf")
print("usage: pdf_rotate.py <spaced delimited list of pages/page ranges>\nexample: pdf_rotate.py 9-12 15 17-19\nwill rotate pages 9 10 11 12 15 17 18 and 19")
exit(1)
for inp in inps:
if "-" in inp:
sinp = inp.split("-")
[rotatepages.append(n) for n in range(int(sinp[0])-1,int(sinp[1]))]
else:
rotatepages.append(int(inp)-1)
if input(f"rotating these pages of inp.pdf by 90 degrees clockwise:\n{rotatepages}\nis this okay? y/N").lower() != "y":
print("did nothing, exiting")
exit(0)
with open('input.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
for p in rotatepages:
page = reader.pages[p].rotate(90)
writer = PyPDF2.PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open('output.pdf', 'wb') as new_file:
writer.write(new_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment