How to Replace Text in Word Documents Using Python: Fast and Effective

Find and Replace is a handy feature for updating specific information or correcting errors in Word documents. While MS Word offers this functionality, handling multiple documents manually can be time-consuming. A programmatic solution, such as using Python, provides a faster and more efficient way to tackle this task. In this article, we’ll show you how to replace text in Word documents using Python, helping you streamline document management.
Python Library to Replace Text in Word Documents
Python alone isn’t sufficient to efficiently complete this task; you’ll need additional Python libraries such as Spire.Doc or Aspose.Doc. In this article, we’ll use Spire.Doc to demonstrate how to find and replace text in a Word document, thanks to its simple and intuitive API. You can install it using the pip command: pip install Spire.Doc.
How to Replace All Instances of Text in Word Files
After the preparation, it is time to get down to the real business, how to replace text in Word documents. There are various situations where replacing all occurrences of a specific text is necessary, such as working with templates, correcting spelling errors, or updating outdated information.
Fortunately, Spire.Doc (short for Spire.Doc for Python) simplifies this process with its Document.Replace() method. Let’s dive into the specific steps to accomplish this task efficiently.
Steps to replace all occurrences of text in Word documents:
Create a Document object and use the Document.LoadFromFile() method to read a Word document from the local storage.
Find the specified text and replace all occurrences with the Document.Replace() method.
Save the updated document as a new one using the Document.SaveToFile() method.
Here is the code example of replacing the word “Spire.Doc” with “Eiceblue” in a Word document:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")
# Find a specific text and replace all its instances with another text
document.Replace("Spire.Doc", "Eiceblue", False, True)
# Save the resulting document
document.SaveToFile("ReplaceAllOccurrencesOfText.docx", FileFormat.Docx2016)
document.Close()
Python: Replacing the First Instance of Text in Word Documents
There are occasions when you may only need to replace the first instance of a specific text. For example, a Word document with repetitive patterns but requires selective changes. Replacing the first occurrence of text in Word files using Python is easy. You only need to make a little change to the Document.Replace() method. Let’s check it out in the following steps.
Steps to find and replace the first instance of text in Word documents:
Create an object of the Document class, and load a Word document from the local storage with the Document.LoadFromFile() method.
Change the replacement mode to replace the first instance by setting the Document.ReplaceFirst property to
True.Find the specified text and replace the first occurrence of the specified text with the Document.Replace() method.
Save the resulting document using the Document.SaveToFile() method.
Below is an example of replacing the first “Spire.Doc” with “Eiceblue” in a Word document:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")
# Change the replacement mode to replace the first match
document.ReplaceFirst = True
# Replace the first instance of a text with another text
document.Replace("Spire.Doc", "Eiceblue", False, True)
# Save the resulting document
document.SaveToFile("ReplaceFirstOccurrenceOfText.docx", FileFormat.Docx2016)
document.Close()
How to Replace Text in Word Files Using Regular Expressions
Sometimes, the text you need to replace follows a specific pattern rather than being identical, such as phone numbers, ZIP codes, email addresses, or dates. Fortunately, you can use regular expressions (Regex) to identify and replace such text efficiently. With Spire.Doc, this process is simplified: you just need to create a Regex object, define the appropriate pattern, and use it for replacement.
Steps to find and replace text in Word files using regular expressions:
Create an instance of the Document class, and load a Word document with the Document.LoadFromFile() method.
Create a Regex object and define the pattern of text to be replaced.
Find and replace the text with new text using the Document.Replace() method.
Save the modified Word file by calling the Document.SaveToFile() method.
Here is an example of replacing the pattern “#Name” with “Spire.Doc for Python”:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample1.docx")
# Create a regex to match the text that starts with #
regex = Regex("""\\#\\w+\\b""")
# Find the text matching the regex and replace it with another text
document.Replace(regex, "Spire.Doc for Python")
# Save the document
document.SaveToFile("ReplaceTextUsingRegex.docx", FileFormat.Docx2016)
document.Close()
How to Replace Text in Word Documents with Images
Another common replacement scenario involves replacing text with images. This is particularly useful when you need to insert the same image repeatedly, such as a company logo or a product image. Instead of manually inserting the image multiple times while creating the document, you can streamline the process by completing the text and replacing it with the desired image in one go.
Steps to replace text with images in Word documents:
Create a Document instance, and load a Word document from files with the Document.LoadFromFile() method.
Find the specified text in the document using the Document.FindAllString() method.
Loop through all finding results.
Create a DocPicture instance and load an image using the DocPicture.LoadImage() method.
Get the found text as a single text range and then get the index of the text range in its owner paragraph.
Insert an image at the position of the text range and then remove the text range from the document.
Save the resulting document using the Document.SaveToFile() method.
Here is the code example of replacing the word “Spire.Doc” with an image:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")
# Find a specific text in the document
selections = document.FindAllString("Spire.Doc", True, True)
index = 0
testRange = None
# Loop through the found results
for selection in selections:
# Load an image
pic = DocPicture(document)
pic.LoadImage("logo.png")
# Get the found text as a single text range
testRange = selection.GetAsOneRange()
# Get the index of the text range in its owner paragraph
index = testRange.OwnerParagraph.ChildObjects.IndexOf(testRange)
# Insert an image at the index
testRange.OwnerParagraph.ChildObjects.Insert(index, pic)
# Remove the text range
testRange.OwnerParagraph.ChildObjects.Remove(testRange)
# Save the resulting document
document.SaveToFile("ReplaceTextWithImage.docx", FileFormat.Docx2016)
document.Close()
The Conclusion
This article provides a comprehensive guide on replacing text in Word documents, covering four key scenarios: replacing all occurrences, replacing the first instance, using regular expressions for flexible replacements, and replacing text with images. With these step-by-step instructions, you'll find it easy to handle a wide range of replacement tasks efficiently and effectively!