How to split a single Word DOCX into Separate Documents by Page in Python

Prepare yourself for the mountain of tedium that is splitting DOCX files into single pages in Python. Just kidding, I’ve got a shortcut for you.

To speed things along today, we need to use an API. Set up its client via pip install:

pip install cloudmersive-convert-api-client

Next up is our function call for split_document_docx, making use of an API instance, like so:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest 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 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 Pageapi_response = api_instance.split_document_docx(input_file, return_document_contents=return_document_contents)pprint(api_response)except ApiException as e:print("Exception when calling SplitDocumentApi->split_document_docx: %s\n" % e)

The only thing left is to submit our request to the server, after which we will receive our response containing all of our pages as individual DOCX files. Cake!

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