Adding Highlighted Text

Text is highlighted by assigning a member of WD_COLOR_INDEX to Font.highlight_color. To use WD_COLOR_INDEX we have first import it using the following import statement.

from docx.enum.text import WD_COLOR_INDEX

Syntax: paragraph.add_run().font.highlight_color = Colour_Name

Parameter: Colour_Name: It is the name of the colour assign to highlight the text. It is the member of the WD_COLOR_INDEX.

Note: If we don’t assign the colour then default colour is NONE.

Example 1: Highlighting the complete paragraph using the python-docx module.

Python3




# Import docx NOT python-docx
import docx
from docx.enum.text import WD_COLOR_INDEX
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('w3wiki', 0)
  
# Creating paragraph with some content and Highlighting it.
highlight_para = doc.add_paragraph(
       ).add_run(
           '''w3wiki is a Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes etc.'''
                 ).font.highlight_color = WD_COLOR_INDEX.YELLOW
  
# Now save the document to a location 
doc.save('gfg.docx')


Output:

Example 2: Highlighting the specific word or phrase in a paragraph using the python-docx module.

Python3




# Import docx NOT python-docx
import docx
from docx.enum.text import WD_COLOR_INDEX
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('w3wiki', 0)
  
# Creating paragraph with some content
para = doc.add_paragraph('''w3wiki is a Computer Science portal for geeks.''')
  
# Adding more content to paragraph and highlighting them
para.add_run(''' It contains well written, well thought and well-explained '''
            ).font.highlight_color = WD_COLOR_INDEX.YELLOW
  
# Adding more content to paragraph
para.add_run('''computer science and programming articles, quizzes etc.''')
  
# Now save the document to a location 
doc.save('gfg.docx')


Output:

Working with Highlighted Text in Python .docx Module

Prerequisites: docx

Word documents contain formatted text wrapped within three object levels. The Lowest level- run objects, middle level- paragraph objects and highest level- document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module. Pip command to install this module is:

pip install python-docx

Python docx module allows users to manipulate docs by either manipulating the existing one or creating a new empty document and manipulating it. It is a powerful tool as it helps you to manipulate the document to a very large extend. You can also add highlighted text in your Word document.

To add the highlighted text you have to first create a paragraph object then you have to use add_run() method to add content. You can directly use add_paragraph() method to add a paragraph but if you want to add the highlighted text you have to use add_run() as all the block-level formatting is done by using add_paragraph() method while all the character-level formatting is done by using add_run().

Similar Reads

Adding Highlighted Text

Text is highlighted by assigning a member of WD_COLOR_INDEX to Font.highlight_color. To use WD_COLOR_INDEX we have first import it using the following import statement....

Styles

...

Contact Us