Saturday, November 16, 2024
Google search engine
HomeLanguagesWorking with Highlighted Text in Python .docx Module

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().

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('GeeksForGeeks', 0)
  
# Creating paragraph with some content and Highlighting it.
highlight_para = doc.add_paragraph(
       ).add_run(
           '''Lazyroar is a Computer Science portal for Lazyroar. 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('GeeksForGeeks', 0)
  
# Creating paragraph with some content
para = doc.add_paragraph('''Lazyroar is a Computer Science portal for Lazyroar.''')
  
# 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:

Styles

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-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('GeeksForGeeks', 0)
  
# Adding Auto Styled Highlighted paragraph
doc.add_heading('AUTO Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.AUTO
  
# Adding Black Styled Highlighted paragraph
doc.add_heading('BLACK Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.BLACK
  
# Adding Blue Styled Highlighted paragraph
doc.add_heading('BLUE Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.BLUE
  
# Adding Bright Green Styled Highlighted paragraph
doc.add_heading('BRIGHT_GREEN Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.BRIGHT_GREEN
  
# Adding Dark Blue Styled Highlighted paragraph
doc.add_heading('DARK_BLUE Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.DARK_BLUE
  
# Adding Dark Red Styled Highlighted paragraph
doc.add_heading('DARK_RED Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.DARK_RED
  
# Adding Dark Yellow Styled Highlighted paragraph
doc.add_heading('DARK_YELLOW Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.DARK_YELLOW
  
# Adding GRAY25 Styled Highlighted paragraph
doc.add_heading('GRAY_25 Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.GRAY_25
  
# Adding GRAY50 Styled Highlighted paragraph
doc.add_heading('GRAY_50 Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.GRAY_50
  
# Adding GREEN Styled Highlighted paragraph
doc.add_heading('GREEN Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.GREEN
  
# Adding Pink Styled Highlighted paragraph
doc.add_heading('PINK Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.PINK
  
# Adding Red Styled Highlighted paragraph
doc.add_heading('RED Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.RED
  
# Adding Teal Styled Highlighted paragraph
doc.add_heading('TEAL Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.TEAL
  
# Adding Turquoise Styled Highlighted paragraph
doc.add_heading('TURQUOISE Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.TURQUOISE
  
# Adding Violet Styled Highlighted paragraph
doc.add_heading('VIOLET Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.VIOLET
  
# Adding White Styled Highlighted paragraph
doc.add_heading('WHITE Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.WHITE
  
# Adding Yellow Styled Highlighted paragraph
doc.add_heading('YELLOW Style:', 3)
doc.add_paragraph().add_run('Lazyroar is a Computer Science portal for Lazyroar.'
                  ).font.highlight_color = WD_COLOR_INDEX.YELLOW
  
# Now save the document to a location 
doc.save('gfg.docx')


Output:

RELATED ARTICLES

Most Popular

Recent Comments