The Find and Replace feature in Word offers a reliable and efficient solution for updating text within your documents. It eliminates the need for exhaustive manual searching and editing by automatically locating and replacing the desired text throughout the entire document. This not only saves time but also guarantees that every instance of the targeted text is updated consistently. In this article, we will demonstrate how to find and replace text in a Word document in Python using Spire.Doc for Python.
- Find Text and Replace All Its Instances with New Text
- Find Text and Replace Its First Instance with New Text
- Find and Replace Text Using a Regular Expression
- Find and Replace Text with an Image
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Find Text and Replace All Its Instances with New Text
You can find a text and replace all its instances with another text easily using the Document.Replace() method. The detailed steps are as follows:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Find a specific text and replace all its instances with another text using Document.Replace() method.
- Save the resulting document using Document.SaveToFile() method.
- Python
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()
Find Text and Replace Its First Instance with New Text
Spire.Doc for Python provides the Document.ReplaceFirst property which enables you to change the replacement mode from replacing all instances to replacing the first instance. The following steps explain how to find a text and replace its first instance in a Word document:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Change the replacement mode to replace the first instance by setting the Document.ReplaceFirst property as True.
- Replace the first instance of a text with another text using Document.Replace() method.
- Save the resulting document using Document.SaveToFile() method.
- Python
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()
Find and Replace Text Using a Regular Expression
You can replace a text matching a regular expression with new text by passing a Regex object and the new text to the Document.Replace() method as parameters. The detailed steps are as follows:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Create a Regex object to match the specific text.
- Replace the text matching the regex with another text using Document.Replace() method.
- Save the resulting document using Document.SaveToFile() method.
- 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()
Find and Replace Text with an Image
Spire.Doc for Python doesn't offer a direct method to replace text with image, but you can achieve this by inserting the image at the position of the text and then removing the text from the document. The detailed steps are as follows:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Find a specific text in the document using Document.FindAllString() method.
- Loop through the found results.
- Create a DocPicture object and load an image using 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 Document.SaveToFile() method.
- Python
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()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.