Rotate PDF via Python

Change PDF page orientation. Use Aspose.PDF for Python for .NET to modify PDF documents programmatically

Rotate PDF Pages with Python

Do you need to rotate PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. With Python libraries like Aspose.PDF – are stand-alone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional Python developers.

This Python library enables developers to rotate PDF documents quickly and efficiently through programming. This library shows you how easily to update or change the page orientation of pages in an existing PDF file programmatically with Python. It is a standalone solution for processing PDF documents in Python that does not require the installation of any third-party software.

In order to rotate PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy-to-use document manipulation API for Python via .NET platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

How to Rotate PDF documents


You need Aspose.PDF for Python to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Move page upper in order to compensate changing page size.
  3. Set old and new page height.
  4. Setting the page rotation angle.
  5. Save the output PDF file.

If you want to rotate PDF files for your tasks, this solution can help. With this powerful Aspose.PDF Python for .NET library, you can update or change the page orientation of pages while keeping its quality. This professional tool uses Python to rotate PDF effectively. The following example demonstrates how to rotate PDF document in Python:

Rotate PDF - Python

This sample code shows how to Rotate PDF Pages - Python

Input file:

File not added

Output format:

PDF

Output file:


doc = ap.Document(input_pdf)
for page in doc.pages:
    r = page.media_box
    newHeight = r.width
    newWidth = r.height
    newLLX = r.llx
    #  We must to move page upper in order to compensate changing page size
    # (lower edge of the page is 0,0 and information is usually placed from the
    #  Top of the page. That's why we move lover edge upper on difference between
    #  Old and new height.
    newLLY = r.lly + (r.height - newHeight)
    page.media_box = ap.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight, True)
    # Sometimes we also need to set CropBox (if it was set in original file)
    page.crop_box = ap.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight, True)

    # Setting Rotation angle of page
    page.rotate = ap.Rotation.ON90

# Save output file
doc.save(output_pdf)

About Aspose.PDF for Python API

Aspose.PDF for Python via .NET API supports most established PDF standards and PDF specifications. It allows developers to insert tables, graphs, images, hyperlinks, custom fonts - and more - into PDF documents. Moreover, it is also possible to rotate PDF documents. Aspose.PDF for Python via .NET provides excellent security features to develop secure PDF documents. Some of the key features of Aspose.PDF for Python via .NET API include:

  • Ability to read & export PDF in multiple image formats including BMP, GIF, JPEG & PNG.
  • Set basic information (e.g. author, creator) of the PDF document.
  • Conversion Features: Convert PDF to Word, Excel, and PowerPoint. Convert PDF to Images formats. Convert PDF file to HTML format and vice versa. Convert PDF to EPUB, Text, XPS, etc.

You can find more information about Aspose.PDF for Python via .NET API on our documentation on how to use API.