for file in os.listdir("Data"): if file.endswith(".pdf"): rotatepdf(f"Data/{file}",…
- User
for file in os.listdir("Data"): if file.endswith(".pdf"): rotate_pdf(f"Data/{file}", f"{output_dir}/{file[:-4]}r.pdf")
How to rotate files but in range of 1,360, what library to use
- Assistant
To rotate PDF files within a specific range of degrees (1 to 360), you can use the
PyPDF2library in Python. Here's how you can achieve that:-
Install PyPDF2 if you haven't already:
pip install PyPDF2 -
Use the following code to rotate PDF files by specifying a rotation angle. You can replace
rotation_anglewith any value from 1 to 360:
import os from PyPDF2 import PdfReader, PdfWriter def rotate_pdf(input_path, output_path, rotation_angle): reader = PdfReader(input_path) writer = PdfWriter() for page in reader.pages: page.rotate_clockwise(rotation_angle) writer.add_page(page) with open(output_path, "wb") as output_pdf: writer.write(output_pdf) # Directory paths input_dir = "Data" output_dir = "RotatedPDFs" # Ensure this directory exists or create it # Ensure the output directory exists if not os.path.exists(output_dir): os.makedirs(output_dir) # Choose the rotation angle (1 to 360 degrees) rotation_angle = 90 # Example: Rotate by 90 degrees for file in os.listdir(input_dir): if file.endswith(".pdf"): rotate_pdf(f"{input_dir}/{file}", f"{output_dir}/{file[:-4]}r.pdf", rotation_angle) print("PDF rotation completed.") -
- User
DeprecationError Traceback (most recent call last) Cell In[28], line 28 26 for file in os.listdir(input_dir): 27 if file.endswith(".pdf"): ---> 28 rotate_pdf(f"{input_dir}/{file}", f"{output_dir}/{file[:-4]}r.pdf", rotation_angle) 30 print("PDF rotation completed.")
Cell In[28], line 9, in rotate_pdf(input_path, output_path, rotation_angle) 6 writer = PdfWriter() 8 for page in reader.pages: ----> 9 page.rotate_clockwise(rotation_angle) 10 writer.add_page(page) 12 with open(output_path, "wb") as output_pdf:
File /opt/conda/lib/python3.10/site-packages/PyPDF2/_page.py:552, in PageObject.rotate_clockwise(self, angle) 551 def rotate_clockwise(self, angle: int) -> "PageObject": # pragma: no cover --> 552 deprecation_with_replacement("rotate_clockwise", "rotate", "3.0.0") 553 return self.rotate(angle)
File /opt/conda/lib/python3.10/site-packages/PyPDF2/_utils.py:369, in deprecation_with_replacement(old_name, new_name, removed_in) 363 def deprecation_with_replacement( 364 old_name: str, new_name: str, removed_in: str = "3.0.0" 365 ) -> None: 366 """ 367 Raise an exception that a feature was already removed, but has a replacement. 368 """ --> 369 deprecation(DEPR_MSG_HAPPENED.format(old_name, removed_in, new_name))
- Assistant
The error you're encountering is due to a deprecated method name in PyPDF2. In the latest versions of PyPDF2 (3.0.0 and above), the
rotate_clockwiseandrotate_counter_clockwisemethods have been replaced with a more genericrotatemethod. Here's how you can update your code to fix the deprecation issue:import os from PyPDF2 import PdfReader, PdfWriter def rotate_pdf(input_path, output_path, rotation_angle): reader = PdfReader(input_path) writer = PdfWriter() for page in reader.pages: page.rotate(rotation_angle) writer.add_page(page) with open(output_path, "wb") as output_pdf: writer.write(output_pdf) # Directory paths input_dir = "Data" output_dir = "RotatedPDFs" # Ensure this directory exists or create it # Ensure the output directory exists if not os.path.exists(output_dir): os.makedirs(output_dir) # Choose the rotation angle (1 to 360 degrees) rotation_angle = 90 # Example: Rotate by 90 degrees for file in os.listdir(input_dir): if file.endswith(".pdf"): rotate_pdf(f"{input_dir}/{file}", f"{output_dir}/{file[:-4]}r.pdf", rotation_angle) print("PDF rotation completed.") - User
find different library
- Assistant
If you encounter issues with
PyPDF2or prefer using a different library, you can consider usingreportlabin combination withpdfplumber. Here's how you can rotate PDF pages using these libraries:-
Install the required libraries:
pip install reportlab pdfplumber
-
- User
import torch import torchvision import torchvision.transforms as T from PIL import Image
use this library
- Assistant
- User
for file in os.listdir(input_dir): if file.endswith(".pdf"): rotate_pdf(f"{input_dir}/{file}", f"{output_dir}/{file[:-4]}r.pdf", rotation_angle) from pdf2image import convert_from_path import torchvision.transforms as T from PIL import Image
def pdf_to_images(pdf_path): images = convert_from_path(pdf_path) return images
def rotate_image(image, angle): transform = T.Compose([ T.functional.rotate(angle), ]) return transform(image)
def images_to_pdf(images, output_path): if images: images[0].save(output_path, save_all=True, append_images=images[1:], format='PDF')
pdf_path = "example.pdf" output_pdf_path = "rotated_example.pdf" rotation_angle = 30
image_pages = pdf_to_images(pdf_path)
rotated_images = [rotate_image(img, rotation_angle) for img in image_pages]
images_to_pdf(rotated_images, output_pdf_path)
- Assistant
- User
I want the files to be 1920x1080, and else to be white
- Assistant