How to split a PDF file into separate page PDF files in Python

Our goal today is to take all the pain and suffering out of splitting up PDF files in Python. We can accomplish this quite simply by applying an API to the problem. Let me demonstrate the process.

First, install the client via pip install:

pip install cloudmersive-convert-api-client

And from there we can write out our function call, which will make use of an API instance, like so.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.SplitDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.return_document_contents = true # bool | Set to true to directly return all of the document contents in the DocumentContents field; set to false to return contents as temporary URLs (more efficient for large operations).  Default is false. (optional)try:# Split a PDF file into separate PDF files, one per pageapi_response = api_instance.split_document_pdf_by_page(input_file, return_document_contents=return_document_contents)pprint(api_response)except ApiException as e:print("Exception when calling SplitDocumentApi->split_document_pdf_by_page: %s\n" % e)

Once your input your file path, it’s just a matter of submitting the request to the server. Not long after that, you will receive your individual page PDFs. Super simple.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store