Learn how to find and replace text in Word documents using Python:
Last active
November 10, 2021 02:54
-
-
Save aspose-com-gists/88b68c48e4cf6a2d3f576c5395b17099 to your computer and use it in GitHub Desktop.
Find and Replace Text in Word Documents using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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