How to add a page number to the footer in a Word DOCX document in Python

I don’t know about you, but dealing with DOCX footers is not my idea of a fun afternoon. This is why I will be taking this opportunity to show you how to use an API to add page numbers into your DOCX footers with Python. This is going to save you so much time!

To use our API in your project, we need to install the client first:

pip install cloudmersive-convert-api-client

The second step is writing a call for our function edit_document_docx_set_footer_add_page_number. This is illustrated in the below example, letting you see how to instance the API, etc.

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.EditDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))req_config = cloudmersive_convert_api_client.DocxSetFooterAddPageNumberRequest() # DocxSetFooterAddPageNumberRequest | Document input requesttry:# Add page number to footer in a Word DOCX documentapi_response = api_instance.edit_document_docx_set_footer_add_page_number(req_config)pprint(api_response)except ApiException as e:print("Exception when calling EditDocumentApi->edit_document_docx_set_footer_add_page_number: %s\n" % e)

Well that was easy. And now for the moment we’ve all been waiting for: it’s time to go celebrate all the time you’ve saved over a cup of coffee!

Image for post
Image for post

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