DEV Community

Tilal Ahmad Sana
Tilal Ahmad Sana

Posted on

1

Search and Replace Text in Microsoft Word Document using Python

If you are automating MS Word documents with Python, then you know that search and replace placeholders/text in documents is a key feature of automation. In this tutorial, I will show you how easily you can add search and replace text feature in your application.

We will use Aspose.Words Cloud SDK for Python for the purpose. You can use Aspose.Words REST API to create a new Word document from scratch, edit, split, merge, compare and convert existing Word documents, but we will only focus on search and replace feature in this tutorial. So let’s start.
Here are the Steps to Search and Replace Text in a Word document:

Step 1:
Sign up with aspose.cloud and get App SID and App Key to authenticate your rest API calls.

Step 2:
Install Aspose.Words Cloud SDK for Python from PIP.

Step 3:
Copy and paste the following code into your Python script file. Put your source file in the same directory, amend the Python script as per your file and run. That’s it.

# For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-python
import asposewordscloud
import asposewordscloud.models.requests
from shutil import copyfile

api_client = asposewordscloud.ApiClient()
api_client.configuration.host = 'https://api.aspose.cloud'
# Please get your App Key and App SID from https://dashboard.aspose.cloud/#/apps.
api_client.configuration.api_key['api_key'] = 'xxxxxxxxxxxxxxxxxxxxxx' # Put your appKey here
api_client.configuration.api_key['app_sid'] = 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' # Put your appSid here

words_api = asposewordscloud.WordsApi(api_client)
filename = 'C:/Temp/TestFile.docx'
remoteFileName = 'TestFile.docx'
outputFileName = 'TestFile_output.docx'

#upload DOCX file to storage
request_upload = asposewordscloud.models.requests.UploadFileRequest(filename,remoteFileName)
response_upload = words_api.upload_file(request_upload)

#Search and Replace the Text
requestReplaceText = asposewordscloud.ReplaceTextParameters(old_value='malesuada', new_value='aspose')
request = asposewordscloud.models.requests.ReplaceTextRequest(name=remoteFileName, replace_text=requestReplaceText, dest_file_name=outputFileName)
result = words_api.replace_text(request)

#download file
request_download=asposewordscloud.models.requests.DownloadFileRequest(outputFileName)
response_download = words_api.download_file(request_download)
copyfile(response_download, 'C:/Temp/'+ outputFileName)
print("Result {}".format(result))

Hope this helps. If you have any question or suggestion, please feel free to comment here or Aspose.Words Cloud forum.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay