How to Split a PDF File into Separate PDF Files (One per Page) using Python
All pages in a PDF document are not necessarily created equally. It’s common to split multi-page PDF documents into individual pages based on the contents of each page, and thankfully, there’s an API for that. Our PDF splitter API will automatically create new files or temporary URLs for each individual page within a PDF document, making next steps in your document processing application that much more efficient. Below, I’ll demonstrate how you can incorporate this API using ready-to-run Python code to structure your API call. To use this API, you’ll need a Cloudmersive API key, which you can get for free by registering a free account on our website.
Let’s begin by running the below command to install the Python SDK:
pip install cloudmersive-convert-api-client
Now let’s add the imports:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
Last, let’s include the API key authorization snippet, followed by the remaining code to call the function:
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.SplitDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # 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 page
api_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)
That concludes our demo — no more code required.