从单词表中提取突出显示的文本-将提取的文本与行索引匹配

问题描述

我正试图从Word文档中的表格中提取突出显示的文本,设法做到了:

from docx import *
document = Document(file_loc)
words = 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'

highlight = []
for word in words:
    for rPr in word.findall(tag_rPr):
        high=rPr.findall(tag_highlight)
        for hi in high:
            if hi.attrib[tag_val] in ['yellow','blue','green','red']:
                 highlight.append(word.find(tag_t).text.encode('utf-8'))

输出为:

highlight = ['***1','***2','***3','***4']

还要标识突出显示的行的索引,例如:

index = []
document = Document(file_loc)
table = document.tables[0]
par = table.rows
for i in range(len(par)):
    if 'highlight' in table.cell(i,4)._tc.xml:
         index.append(i-1)

index = [5,8]

但是,在1个选定的索引中有多个突出显示的句子,因此提取的文本和索引无法直接匹配,如何将行索引与多个突出显示的文本进行匹配

{5: ['xxx1','xxx2'],8: ['xxx3','xxx4']}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)