How to Rotate PDF Pages in Python

Cloudmersive
2 min readJan 4, 2023

--

While it’s simple enough to make “point and click” edits to any PDF document on an individual basis, it’s much more complicated to make PDF edits at scale as part of an application’s file processing workflow. That’s why the below “Rotate PDF Pages” API solution can make a big difference: it’ll allow you to quickly integrate a solution which programmatically rotates all pages in your PDF documents, only requiring a few lines of ready-to-run Python code to make your API call. This API requires the following request parameters:

  1. inputFile (input file to perform the operation on)
  2. rotationAngle (multiples of 90 degrees; positive integers rotate right, negative integers rotate left)
  3. Cloudmersive API key (visit our website and register a free account to get one; these provide a limit of 800 API calls per month with no commitments)

You can structure your API call in two simple steps. First, run the following command to install the Python SDK:

pip install cloudmersive-convert-api-client

After that, copy in the remaining code examples:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint

# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'



# create an instance of the API class
api_instance = cloudmersive_convert_api_client.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
rotation_angle = 56 # int | The angle to rotate the page in degrees, must be a multiple of 90 degrees, e.g. 90, 180, 270, or -90, -180, -270, etc.

try:
# Rotate all pages in a PDF document
api_response = api_instance.edit_pdf_rotate_all_pages(input_file, rotation_angle)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditPdfApi->edit_pdf_rotate_all_pages: %s\n" % e)

That’s all there is to it!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet