NameError:使用python-docx时未定义名称“ WD_COLOR_INDEX”

H W*_*H W 2 python ms-word highlight python-docx

我一直在尝试使用以下方法在MS Word文档中找到突出显示的颜色 python-docx(python-docx-0.8.6,python 2.7、32位)并根据其突出显示颜色处理文本的每一部分。

根据文档,我尝试导入/使用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
Run Code Online (Sandbox Code Playgroud)

如何导入颜色索引?

sca*_*nny 6

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

from docx.enum.text import WD_COLOR_INDEX
Run Code Online (Sandbox Code Playgroud)

它还有一个别名(用于更简洁的表达),因此您可以改用以下别名:

from docx.enum.text import WD_COLOR
Run Code Online (Sandbox Code Playgroud)

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