我正在处理一堆Word文档,其中有突出显示的文本(单词)(使用颜色代码,例如黄色、蓝色、灰色),现在我想提取与每种颜色相关的突出显示的单词。我正在用 Python 编程。这是我目前所做的:
打开word文档[python-docx][1]
然后到达<w:r>
包含文档中标记(单词)的标签。我使用了以下代码:
#!/usr/bin/env python2.6
# -*- coding: ascii -*-
from docx import *
document = opendocx('test.docx')
words = document.xpath('//w:r', namespaces=document.nsmap)
for word in words:
print word
现在我陷入了检查每个单词是否有的部分<w:highlight>
标记并从中提取颜色代码,以及它是否与内部的黄色打印文本匹配<w:t>
标签。如果有人能指出我从解析的文件中提取单词,我将非常感激。
我以前从未与python-docx,但有帮助的是我在网上找到了一段突出显示的文本的 XML 结构的片段:
<w:r>
<w:rPr>
<w:highlight w:val="yellow"/>
</w:rPr>
<w:t>text that is highlighted</w:t>
</w:r>
从那里,想出这个相对简单:
from docx import *
document = opendocx(r'test.docx')
words = document.xpath('//w:r', namespaces=document.nsmap)
WPML_URI = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
tag_rPr = WPML_URI + 'rPr'
tag_highlight = WPML_URI + 'highlight'
tag_val = WPML_URI + 'val'
for word in words:
for rPr in word.findall(tag_rPr):
if rPr.find(tag_highlight).attrib[tag_val] == 'yellow':
print word.find(tag_t).text
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)