📜  在Python .docx 模块中处理高亮文本

📅  最后修改于: 2022-05-13 01:55:18.918000             🧑  作者: Mango

在Python .docx 模块中处理高亮文本

先决条件: docx

Word 文档包含包装在三个对象级别内的格式化文本。最低级-运行对象,中间级-段落对象和最高级-文档对象。因此,我们无法使用普通文本编辑器处理这些文档。但是,我们可以使用 python-docx 模块在Python中操作这些 word 文档。安装这个模块的pip命令是:

pip install python-docx

Python docx 模块允许用户通过操作现有文档或创建新的空文档并对其进行操作来操作文档。它是一个强大的工具,因为它可以帮助您在非常大的范围内操作文档。您还可以在 Word 文档中添加突出显示的文本。

要添加突出显示的文本,您必须首先创建一个段落对象,然后您必须使用 add_run() 方法来添加内容。您可以直接使用 add_paragraph() 方法添加段落,但是如果要添加突出显示的文本,则必须使用 add_run() 因为所有块级格式化都是通过使用 add_paragraph() 方法完成的,而所有字符级格式化是通过使用 add_run() 完成的。

添加突出显示的文本

通过将 WD_COLOR_INDEX 的成员分配给 Font.highlight_color 来突出显示文本。要使用 WD_COLOR_INDEX,我们首先使用以下导入语句导入它。

from docx.enum.text import WD_COLOR_INDEX

示例 1:使用 python-docx 模块突出显示完整的段落。

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


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('''GeeksforGeeks 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')


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('GeeksforGeeks is a Computer Science portal for geeks.'
                  ).font.highlight_color = WD_COLOR_INDEX.AUTO
  
# Adding Black Styled Highlighted paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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')


输出:

示例 2:使用 python-docx 模块突出显示段落中的特定单词或短语。

蟒蛇3

# 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('''GeeksforGeeks 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')

输出:

样式

有多种颜色选项可以突出显示文本。您可以选择属于 WD_COLOR_INDEX成员的任何颜色。颜色列表如下:

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

示例 3:在 Word 文档中添加样式突出显示的文本。

蟒蛇3

# 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('GeeksforGeeks is a Computer Science portal for geeks.'
                  ).font.highlight_color = WD_COLOR_INDEX.AUTO
  
# Adding Black Styled Highlighted paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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 paragraph
doc.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')

输出: