How to Split a Single Word DOCX Document into Separate Documents (by Page) using Python
It’s sometimes most efficient to upload or share a single DOCX document containing separate or unrelated information on each page. On the receiving end of that, it’s equally efficient to separate those documents by page — especially with a document conversion service to handle the repetitive task for you. Our DocX Split API will make that separation for you, creating new single-page documents based on each page in the original document respectively. This API also contains an optional parameter which can be configured to receive URLs to the resulting documents, rather than physical document files.
To help you take advantage of this API for free, we’ve provided ready-to-run code examples in Python which you can use to structure your API call. You’ll also need a Cloudmersive API key to authenticate, which can be acquired by registering a free account on our website.
Let’s start by first installing the Python SDK with the following command:
pip install cloudmersive-convert-api-client
Next, let’s include the following imports above the API key authorization snippet:
from __future__ import print_function
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'
Lastly, let’s 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 return the contents of each Worksheet directly, set to false to only return URLs to each resulting document. Default is true. (optional)try:
# Split a single Word Document DOCX into Separate Documents by Page
api_response = api_instance.split_document_docx(input_file, return_document_contents=return_document_contents)
except ApiException as e:
print("Exception when calling SplitDocumentApi->split_document_docx: %s\n" % e)
And that’s it — no more code required.