我正在尝试从 Word 文档 docx 中读取文本,并尝试查找所有以黄色突出显示的文本,但它给了我一条错误消息
import docx
document = docx.Document(r'C:/Users/devff/Documents/Prac2.docx')
rs = document._element.xpath("//w:r")
WPML_URI = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
tag_rPr = WPML_URI + 'rPr'
tag_highlight = WPML_URI + 'highlight'
tag_val = WPML_URI + 'val'
tag_t = WPML_URI + 't'
for word in rs:
for rPr in word.findall(tag_rPr):
high = rPr.findall(tag_highlight)
for hi in high:
if hi.attribute[tag_val] == 'yellow': ##here is the problem
print(word.find(tag_t).text.encode('utf-8').lower())
理想情况下,它应该打印出突出显示为黄色的文本,但它只是给我:
AttributeError: 'CT_Highlight' object has no attribute 'attribute'
最佳答案
我认为您正在寻找 .attrib
,而不是 .attribute
。
解决这个问题将使您进入下一步,但是您构建它的方式不太可靠,因为如果不存在 val
属性,它会引发异常。我推荐 _Element.get()
https://lxml.de/api/lxml.etree._Element-class.html如果不存在具有请求名称的属性,则仅返回 None
:
if hi.get(tag_val) == 'yellow':
...
关于python - “CT_Highlight”对象没有属性 'attribute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55959113/