from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger from pathlib import Path import sys, os #------------------------------------------------------------------------------- file_name = str(sys.argv[1]) file_path = os.getcwd() + "\\" + file_name in_pdf = PdfFileReader(str(file_path)) rotPg = [10, 12, 14, 16, 18, 20] j = len(sys.argv) q = int(sys.argv[2]) if j == 4: m = int(sys.argv[3]) if j == 5: m = int(sys.argv[3]) n = int(sys.argv[4]) new_file = file_path.strip(".pdf") + "_rot.pdf" out_pdf = PdfFileWriter() # #Note that the counter i starts with ZERO for i in range(in_pdf.getNumPages()): p = in_pdf.getPage(i) # Rotate pages in specified range: q, m and n are specfied if (j == 5): if (i >= m and i <= n): p.rotateClockwise(q) out_pdf.addPage(p) # Rotate pages from the list - only q and m specified, m < 0 elif (j == 4 and m < 0): if i in rotPg: p.rotateClockwise(q) out_pdf.addPage(p) #Rotate only one page specified on command line: q and m specified, m > 0 elif (j == 4 and m > 0): if (i == m): p.rotateClockwise(q) out_pdf.addPage(p) #Rotate all pages: only q specified else: p.rotateClockwise(q) out_pdf.addPage(p) with open(new_file, 'wb') as f: out_pdf.write(f)