Find and Replace Text in Word Documents using Python

Find and replace text in Word using Python

Quite often, you need to replace a particular text or phrase in the Word documents. MS Word has a built-in feature for such cases and you can replace the desired text with one click. In this article, you will learn how to programmatically find and replace text in Word documents using Python. This could be useful when you need to replace text in a bunch of documents. Also, you can embed this feature in your Python applications to censor the Word documents before sharing.

Python Library to Replace Text in Word Documents

To find and replace text in DOCX or DOC files, we will use Aspose.Words for Python. It is a feature-rich Python library that allows you to create and manipulate MS Word documents seamlessly. You can install the library from PyPI using the following pip command.

pip install aspose-words

Find and Replace Text in a Word Document using Python

The following are the steps to find and replace text in a Word document using Python.

  • Load the document using the Document class.
  • Use Document.range.replace(string, string, aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD)) method to replace the text.
  • Save the updated document using Document.save(string) method.

The following code sample shows how to replace a particular text in a DOCX document using Python.

The following is the input Word document that we used in this example.

Input Word Document

The following is the updated Word document after replacing the text.

Replace a Text in Word Document using Python
Updated Word Document After Replacing Text

Replace Text in Word Documents using Regular Expression

In certain cases, you may want to replace the text that matches a particular pattern. For example, you may want to omit the email addresses written in the documents before sharing them online. For such cases, you can specify a regular expression to replace the matching text occurrences. The following are the steps to replace text in Word documents based on a regular expression.

  • Load the document using the Document class.
  • Create an object of FindReplaceOptions class.
  • Replace text based on regular expression using Document.range.replace_regex(string, string, FindReplaceOptions) method.
  • Save the updated document using Document.save(string) method.

The following code sample shows how to replace text in a Word document using a regular expression.

The following is the screenshot of the Word document after replacing text using a RegEx.

Replace text in Word using Regex

Python: Replace Text in Word Documents using Metacharacters

In some cases, a piece of text may be composed of different paragraphs, sections, or pages. To replace such phrases, you need to use the following metacharacters.

  • &p for a paragraph break
  • &b for a section break
  • &m for a page break
  • &l for a line break

The following code sample shows how to use metacharacters to find and replace text in Word documents.

Read more about finding and replacing text in Word documents using Python.

Get a Free API License

You can get a temporary license in order to use Aspose.Words for Python without evaluation limitations.

Conclusion

In this article, you have learned how to find and replace text in Word documents using Python. In addition to that, you have also seen how to use RegEx and metacharacters to replace text in Word documents. Besides this, you can explore other features offered by Aspose.Words for Python using the documentation. Also, you can post your questions on our forum.

See Also