Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active November 10, 2021 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/88b68c48e4cf6a2d3f576c5395b17099 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/88b68c48e4cf6a2d3f576c5395b17099 to your computer and use it in GitHub Desktop.
Find and Replace Text in Word Documents using Python

Learn how to find and replace text in Word documents using Python:

import aspose.words as aw
# load Word document
doc = aw.Document("document.docx")
# create options
findReplaceOptions = aw.replacing.FindReplaceOptions()
findReplaceOptions.apply_paragraph_format.alignment = aw.ParagraphAlignment.CENTER
# double each paragraph break after word "section", add kind of underline and make it centered.
count = doc.range.replace("section&p", "section&p----------------------&p", findReplaceOptions)
# insert section break instead of custom text tag.
count = doc.range.replace("insert-section", "&b", findReplaceOptions)
# save the modified document
doc.save("updated.docx")
import aspose.words as aw
# load Word document
doc = aw.Document("document.docx")
# replace text using RegEx
options = aw.replacing.FindReplaceOptions()
doc.range.replace_regex("[s|m]ad", "bad", options)
# save the modified document
doc.save("updated.docx")
import aspose.words as aw
# load Word document
doc = aw.Document("document.docx")
# replace text
doc.range.replace("sad", "[replaced]", aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD))
# save the modified document
doc.save("updated.docx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment