Working with Highlighted Text in Python .docx Module - GeeksforGeeks

03 Jan, 2021
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().
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-docximport docxfrom docx.enum.text import WD_COLOR_INDEX  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Creating paragraph with some content and Highlighting it.highlight_para = doc.add_paragraph(       ).add_run(           '''GeeksforGeeks 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-docximport docxfrom docx.enum.text import WD_COLOR_INDEX  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Creating paragraph with some contentpara = doc.add_paragraph('''GeeksforGeeks is a Computer Science portal for geeks.''')  # Adding more content to paragraph and highlighting thempara.add_run(''' It contains well written, well thought and well-explained '''            ).font.highlight_color = WD_COLOR_INDEX.YELLOW  # Adding more content to paragraphpara.add_run('''computer science and programming articles, quizzes etc.''')  # Now save the document to a location doc.save('gfg.docx')
Output:
There are various colour option to highlight the text. You can choose any colour which is a member of WD_COLOR_INDEX. The list of colours are as follows:
SR. No.
Colour Name In WD_COLOR_INDEX
Colour Description
1.
AUTO
Default or No Colour
2.
BLACK
Black Colour
3.
BLUE
Blue Colour
4.
BRIGHT_GREEN
Green Colour
5.
DARK_BLUE
Dark Blue Colour
6.
DARK_RED
Dark Red Colour
7.
DARK_YELLOW
Dark Yellow Colour
8.
GRAY_25
Light Gray Colour
9.
GRAY_50
Dark Gray Colour
10.
GREEN
Dark Green Colour
11.
PINK
Magenta Colour
12.
RED
Red Colour
13.
TEAL
Dark Cyan Colour
14.
TURQUOISE
Cyan Colour
15.
VIOLET
Dark Magenta Colour
16.
WHITE
White Colour
17.
YELLOW
Yellow Colour
Example 3: Adding styled highlighted text in the Word document.
Python3
# Import docx NOT python-docximport docxfrom docx.enum.text import WD_COLOR_INDEX  # Create an instance of a word documentdoc = docx.Document()  # Add a Title to the document doc.add_heading('GeeksForGeeks', 0)  # Adding Auto Styled Highlighted paragraphdoc.add_heading('AUTO Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.AUTO  # Adding Black Styled Highlighted paragraphdoc.add_heading('BLACK Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.BLACK  # Adding Blue Styled Highlighted paragraphdoc.add_heading('BLUE Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.BLUE  # Adding Bright Green Styled Highlighted paragraphdoc.add_heading('BRIGHT_GREEN Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.BRIGHT_GREEN  # Adding Dark Blue Styled Highlighted paragraphdoc.add_heading('DARK_BLUE Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.DARK_BLUE  # Adding Dark Red Styled Highlighted paragraphdoc.add_heading('DARK_RED Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.DARK_RED  # Adding Dark Yellow Styled Highlighted paragraphdoc.add_heading('DARK_YELLOW Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.DARK_YELLOW  # Adding GRAY25 Styled Highlighted paragraphdoc.add_heading('GRAY_25 Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.GRAY_25  # Adding GRAY50 Styled Highlighted paragraphdoc.add_heading('GRAY_50 Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.GRAY_50  # Adding GREEN Styled Highlighted paragraphdoc.add_heading('GREEN Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.GREEN  # Adding Pink Styled Highlighted paragraphdoc.add_heading('PINK Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.PINK  # Adding Red Styled Highlighted paragraphdoc.add_heading('RED Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.RED  # Adding Teal Styled Highlighted paragraphdoc.add_heading('TEAL Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.TEAL  # Adding Turquoise Styled Highlighted paragraphdoc.add_heading('TURQUOISE Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.TURQUOISE  # Adding Violet Styled Highlighted paragraphdoc.add_heading('VIOLET Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.VIOLET  # Adding White Styled Highlighted paragraphdoc.add_heading('WHITE Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.WHITE  # Adding Yellow Styled Highlighted paragraphdoc.add_heading('YELLOW Style:', 3)doc.add_paragraph().add_run('GeeksforGeeks is a Computer Science portal for geeks.'                  ).font.highlight_color = WD_COLOR_INDEX.YELLOW  # Now save the document to a location doc.save('gfg.docx')
Output:
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | Get unique values from a list
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()