How to search and replace a Text String in a Word DOCX in Python
Search and replace might not seem like the most difficult of features to implement, but when DOCX format’s complexity becomes a factor, the challenge rises significantly. Instead of trying to set up parsing for DOCX files and then building our search and replace tool to work around that, we will opt for an easier route. With an API at our command, we can be finished almost instantly. Let’s take a look.
The aforementioned API will be needing its client, so let’s import that first.
pip install cloudmersive-convert-api-client
Next we call our function:
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.EditDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))req_config = cloudmersive_convert_api_client.ReplaceStringRequest() # ReplaceStringRequest | Document string replacement configuration inputtry:# Replace string in Word DOCX documentapi_response = api_instance.edit_document_docx_replace(req_config)pprint(api_response)except ApiException as e:print("Exception when calling EditDocumentApi->edit_document_docx_replace: %s\n" % e)
The request object is going to need our input file, as well as our target string and replacement string. And if you now run this code, you will see that our solution is already working. Simple.