NameError: name 'WD_COLOR_INDEX' is not defined while using python-docx

NameError: name 'WD_COLOR_INDEX' is not defined while using python-docx

我一直在尝试使用 python-docx(python-docx-0.8.6,python 2.7,32 位)和过程在 MS Word 文档中找到高亮颜色每段文字基于其高亮颜色。

根据文档,我尝试 import/use WD_COLOR_INDEX,但似乎无法找到它。

from docx.enum import *

if (doc.paragraphs[i].runs[j].font.highlight_color == WD_COLOR_INDEX.YELLOW): 
    #do the appropriate thing for the yellow-highlighted text

如何导入颜色索引?

此枚举与文本相关,因此可在 docx.enum.text 模块中找到:

from docx.enum.text import WD_COLOR_INDEX

它还有一个别名(为了更简洁的表达),所以你可以用这个代替:

from docx.enum.text import WD_COLOR

这使得每个引用更短,例如WD_COLOR.YELLOW.

在python3.7

from docx.enum import *
from docx.enum.text import WD_COLOR_INDEX
if (doc.paragraphs[i].runs[j].font.highlight_color == WD_COLOR_INDEX.YELLOW): 
    #do the appropriate thing for the yellow-highlighted text