Update Table Data in a DOCX File in Python

Cloudmersive
2 min readAug 24, 2021

In this tutorial, we will demonstrate an easy API solution that will allow you to replace placeholder rows in a table in a DOCX file using one or more templates. For the input request, you will need the following information: File URL, File Data, Table Start Tag, Table End Tag, Data to Fill in, Cells, Target Tag (string), and Replacement Value (string). The editing URL that is used is completely secure and will expire within 30 minutes, ensuring your document remains private.

To start things off, we will run this command to install the client:

pip install cloudmersive-convert-api-client

Post-installation, we are ready to configure the API key and call the function with the following example code:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
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'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.TransformDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
request = cloudmersive_convert_api_client.DocxTableTableFillRequest() # DocxTableTableFillRequest |
try:
# Fill in data in a table in a Word DOCX document, return edit session
api_response = api_instance.transform_document_docx_table_fill_in_edit_session(request)
pprint(api_response)
except ApiException as e:
print("Exception when calling TransformDocumentApi->transform_document_docx_table_fill_in_edit_session: %s\n" % e)

Once all edits have been made to the document, we can call the Finish Editing function to download the updated result. To retrieve your API key, head over to the Cloudmersive website to register for a free account; this will provides 800 calls/month across our library of APIs.

--

--

Cloudmersive

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