Python: Find and Highlight Text in Word

The text highlighting feature in MS Word allows users to easily navigate and search for specific sections or content. By highlighting key paragraphs or keywords, users can quickly locate the desired information within the document. This feature is particularly useful when dealing with large documents, as it not only saves time but also minimizes the frustration associated with manual searching, enabling users to focus on the content that truly matters. In this article, we will demonstrate how to find and highlight text in a Word document in Python using Spire.Doc for Python.

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 and Highlight All Instances of a Specified Text in Word in Python

You can use the Document.FindAllString() method provided by Spire.Doc for Python to find all instances of a specified text in a Word document. Then you can loop through these instances and highlight each of them with a bright color using TextRange.CharacterFormat.HighlightColor property. The detailed steps are as follows.

  • Create an object of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Find all instances of a specific text in the document using Document.FindAllString() method.
  • Loop through each found instance, and get it as a single text range using TextSelection.GetAsOneRange() method, then highlight the text range with color using TextRange.CharacterFormat.HighlightColor property.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Specify the input and output file paths
inputFile = "Sample.docx"
outputFile = "HighlightAllInstances.docx"

# Create an object of the Document class
document = Document()

# Load a Word document
document.LoadFromFile(inputFile)

# Find all instances of a specific text
textSelections = document.FindAllString("Spire.Doc", False, True)

# Loop through all the instances
for selection in textSelections:
    # Get the current instance as a single text range
    textRange = selection.GetAsOneRange()
    # Highlight the text range with a color
    textRange.CharacterFormat.HighlightColor = Color.get_Yellow()

# Save the resulting document
document.SaveToFile(outputFile, FileFormat.Docx2016)
document.Close()

Python: Find and Highlight Text in Word

Find and Highlight the First Instance of a Specified Text in Word in Python

You can use the Document.FindString() method to find only the first instance of a specified text and then set a highlight color for it using TextRange.CharacterFormat.HighlightColor property. The detailed steps are as follows.

  • Create an object of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Find the first instance of a specific text using Document.FindString() method.
  • Get the instance as a single text range using TextSelection.GetAsOneRange() method, and then highlight the text range with color using TextRange.CharacterFormat.HighlightColor property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Specify the input and output file paths
inputFile = "Sample.docx"
outputFile = "HighlightTheFirstInstance.docx"

# Create an object of the Document class
document = Document()

# Load a Word document
document.LoadFromFile(inputFile)

# Find the first instance of a specific text
textSelection = document.FindString("Spire.Doc", False, True)

# Get the instance as a single text range
textRange = textSelection.GetAsOneRange()
# Highlight the text range with a color
textRange.CharacterFormat.HighlightColor = Color.get_Yellow()

# Save the resulting document
document.SaveToFile(outputFile, FileFormat.Docx2016)
document.Close() 

Python: Find and Highlight Text in Word

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.