使用python-docx在docx文件中突出显示段落

时间:2018-07-04 07:25:17

标签: python python-3.x python-docx

我正在尝试使用python-docx在.docx文件中突出显示一个段落。

from docx import Document
from docx.enum.text import WD_COLOR_INDEX

source_folder = 'D:/project/data2/'
f = source_folder+'test.docx'
document = Document(f)


for para in document.paragraphs:
    font1 = para.add_run().font
    font1.highlight_color = WD_COLOR_INDEX.RED
document.save(source_folder+'new.docx')

此代码应突出显示文档中的所有段落,但不是。

1 个答案:

答案 0 :(得分:0)

from docx import Document
from docx.enum.text import WD_COLOR_INDEX

source_folder = 'D:/project/data2/'
f = source_folder+'test.docx'
document = Document(f)


for para in document.paragraphs:
    for run in para.runs:
        run.font.highlight_color = WD_COLOR_INDEX.RED
document.save(source_folder+'new.docx')