In daily office work or document processing, we often encounter the problem of incorrect PDF page orientation—for example, scanned documents rotated by 90 degrees, or page orientation that does not meet requirements during typesetting. Manually adjusting each page is not only inefficient but also prone to errors. This article will share how to efficiently and flexibly implement PDF page rotation using Python combined with the Free Spire.PDF for Python library.
Before starting coding, you need to install the library via the pip command first. The installation command is as follows (it is recommended to use a virtual environment to avoid dependency conflicts):
pip install Spire.Pdf.Free
Basic Implementation: Precise Rotation of a Single PDF Page
Taking rotating the first page by 180 degrees as an example, we analyze the core logic with code:
Complete Code
from spire.pdf.common import *
from spire.pdf import *
# 1. Initialize the PDF document object
pdf = PdfDocument()
# 2. Load the target PDF file (ensure the path is correct)
pdf.LoadFromFile("Sample.pdf")
# 3. Get the page to be rotated (index starts from 0, here is the first page)
page = pdf.Pages[0]
# 4. Get the current rotation angle of the page
rotation = int(page.Rotation.value)
# 5. Calculate the new rotation angle (based on original angle + 180 degrees)
rotation += int(PdfPageRotateAngle.RotateAngle180.value)
# 6. Apply the new rotation angle
page.Rotation = PdfPageRotateAngle(rotation)
# 7. Save the processed PDF and release resources
pdf.SaveToFile("RotatePDFPage.pdf")
pdf.Close()
Code Explanation
-
Object Initialization:
PdfDocument()is the core entry point for PDF operations, responsible for loading, editing, and saving documents. -
File Loading:
LoadFromFile()supports reading from local PDF paths or streams; ensure the file exists and the path is correct. -
Page Index:
Pages[0]corresponds to the first page; usePages[1]for the second page, and so on. -
Rotation Angle Control:
PdfPageRotateAngleis an enumeration class that defines standard rotation angles (0/90/180/270 degrees);page.Rotation.valueretrieves the numerical value of the current angle (e.g., 0/90), facilitating dynamic calculation. -
Resource Release:
Close()must be called to avoid memory leaks.
Advanced Operations: Covering Rotation Needs for Multiple Scenarios
1. Rotate to a Specified Angle (90/270 Degrees)
If you need to rotate the page 90 degrees clockwise, simply modify the enumeration value:
from spire.pdf.common import *
from spire.pdf import *
pdf = PdfDocument()
pdf.LoadFromFile("Sample.pdf")
page = pdf.Pages[0]
rotation = int(page.Rotation.value)
# Replace with RotateAngle90 (90 degrees) or RotateAngle270 (270 degrees)
rotation += int(PdfPageRotateAngle.RotateAngle90.value)
page.Rotation = PdfPageRotateAngle(rotation)
pdf.SaveToFile("Rotate90.pdf")
pdf.Close()
2. Batch Rotate All Pages
Traversing the Pages collection enables unified rotation of all pages:
from spire.pdf.common import *
from spire.pdf import *
pdf = PdfDocument()
pdf.LoadFromFile("Sample.pdf")
# Traverse all pages
for page in pdf.Pages:
rotation = int(page.Rotation.value)
rotation += int(PdfPageRotateAngle.RotateAngle180.value)
page.Rotation = PdfPageRotateAngle(rotation)
pdf.SaveToFile("RotateAllPages.pdf")
pdf.Close()
Key Knowledge Point: PdfPageRotateAngle Enumeration
Free Spire.PDF for Python standardizes rotation angles through an enumeration class, avoiding errors from manual value input:
-
RotateAngle0: 0 degrees (no rotation) -
RotateAngle90: 90 degrees clockwise -
RotateAngle180: 180 degrees clockwise -
RotateAngle270: 270 degrees clockwise
Note: Rotation angles are cumulative (e.g., if a page is already rotated 90 degrees, adding another 90 degrees results in 180 degrees).
Free Spire.PDF for Python, with its lightweight design and concise API, perfectly addresses PDF page rotation needs—whether it’s precise adjustment of a single page or unified batch processing, it can be achieved with just a few lines of code.
Top comments (0)