Advertisement
STANAANDREY

rotate pdf even pages

Jun 28th, 2024
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. import PyPDF2
  2.  
  3. def rotate_even_pages(input_pdf, output_pdf):
  4.     # Open the input PDF
  5.     with open(input_pdf, 'rb') as infile:
  6.         reader = PyPDF2.PdfReader(infile)
  7.         writer = PyPDF2.PdfWriter()
  8.        
  9.         # Iterate over all the pages
  10.         num_pages = len(reader.pages)
  11.         for i in range(num_pages):
  12.             page = reader.pages[i]
  13.             # Rotate only the even pages (index 1, 3, 5, etc.)
  14.             if i % 2 == 1:  # 1-based index for even pages: 2, 4, 6, etc.
  15.                 page.rotate(180)
  16.             writer.add_page(page)
  17.        
  18.         # Write the output PDF
  19.         with open(output_pdf, 'wb') as outfile:
  20.             writer.write(outfile)
  21.  
  22. # Example usage
  23. input_pdf = 'input.pdf'  # Replace with your input PDF file
  24. output_pdf = 'output.pdf'  # Replace with your desired output PDF file
  25. rotate_even_pages(input_pdf, output_pdf)
  26.  
  27. print(f"Processed PDF saved as '{output_pdf}'")
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement