How to Quickly Rotate a PDF File in Python and Save Changes


Rotating PDF pages is a common task when handling scanned documents, printing layouts, or correcting file orientation. Instead of manually adjusting each page, using Python allows you to automate the rotation process efficiently. This saves time and reduces the risk of errors. In this guide, I’ll show you how to rotate a PDF file, including rotating specific pages or the entire document, with just a few simple steps using Python.
How to Rotate Specified PDF Pages in Python
Sometimes a PDF file contains full-page images or charts that don't align well with the overall document layout, making printing difficult. In such cases, rotating specific PDF pages can greatly improve the final output. In this section, I’ll show you how to rotate PDF pages in Python using Spire.PDF — a powerful and easy-to-use library. Don’t worry if you use other libraries; the basic process of rotating a PDF remains very similar.
Steps to rotate a PDF page in Python:
Create an object of the PdfDocument class.
Load a PDF document from files using the PdfDocument.LoadFromFile() method.
Get a certain page to be rotated through the PdfDocument.Pages[] property.
Get the current rotation angle with the PdfPageBase.Rotation.value property.
Customize the rotation angle.
Apply the new rotation setting to the page using the PdfPageBase.Rotation property.
Save the modified PDF file with the PdfDocument.SaveToFile() method.
The code example below shows how to rotate the first page by 180 degrees:
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("/Booklet.pdf")
# Get the first page
page = pdf.Pages[0]
# Get the original rotation angle of the page
rotation = int(page.Rotation.value)
# Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += int(PdfPageRotateAngle.RotateAngle180.value)
page.Rotation = PdfPageRotateAngle(rotation)
# Save the result document
pdf.SaveToFile("/RotatePDFPage.pdf")
pdf.Close()
How to Rotate an Entire PDF File with Python
In the previous section, we discussed how to rotate a single page in a PDF file. But what if the entire document needs rotation, such as when a scanned PDF is completely upside down or incorrectly oriented? Fortunately, rotating an entire PDF in Python is just as simple. By traversing each page and adjusting the rotation angle, you can quickly fix the entire document. Let’s take a look!
Steps to rotate PDF files in Python
Create an instance of the PdfDocument class.
Read a PDF document through the PdfDocument.LoadFromFile() method.
Loop through all pages in the PDF.
Retrieve the original rotation angle of each page with the PdfPageBase.Rotation.value property.
Set the rotation angle you want.
Apply the new rotation angle to all PDF pages using the PdfPageBase.Rotation property.
Save the updated PDF as a new PDF document through the PdfDocument.SaveToFile() method.
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("/Booklet.pdf")
# Loop through each page in the document
for i in range(pdf.Pages.Count):
page = pdf.Pages.get_Item(i)
# Get the original rotation angle of the page
rotation = int(page.Rotation.value)
# Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += int(PdfPageRotateAngle.RotateAngle180.value)
page.Rotation = PdfPageRotateAngle(rotation)
# Save the result document
pdf.SaveToFile("/RotatePDF.pdf")
pdf.Close()
The Bottom Line
In this article, we explored how to rotate PDF files in Python, including both individual pages and entire documents. With detailed steps and code examples, you can easily adjust the angle of any PDF file to fit your needs. Start rotating your PDF pages effortlessly today!
Subscribe to my newsletter
Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
