2

いくつかの表がある単語文書があります。各テーブルには、黒と赤の2色があります。

単語ドキュメントテーブルのセルからテキストをその色で取得したいと思います。私は方法を見つけましたが、それは非常に非効率的だと思います。

次のコードは、単語テーブルのセルからテキストを取得し、各単語をその色で出力します。

import os, sys
import win32com.client, re

path = os.path.join(os.getcwd(),"../files/tests2.docx")
word = win32com.client.Dispatch("Word.Application")
word.Visible = 1
doc=word.Documents.Open(path)

for table in doc.Tables:
    f = 2
    c = 2
    wc = table.Cell(f,c).Range.Words.Count
    for i in range(1,wc):
        print table.Cell(f,c).Range.Words(i), table.Cell(f,c).Range.Words(i).Font.Color

これを達成するための他の(より良い)方法を知っていますか?

ありがとうございました。

4

1 に答える 1

3

python-docxを使用して、Word 文書から強調表示された単語を抽出する方法を次に示します。

#!usr/bin/python
# -*- coding: utf-8 -*-
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'
tag_t = WPML_URI + 't'
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] == 'yellow':
                print word.find(tag_t).text.encode('utf-8').lower()
于 2013-01-31T13:05:42.310 に答える