我有一个包含几个表格的word文档。每张桌子有两种颜色:黑色和红色。
我想按颜色从 Word 文档表中的单元格中获取文本。我找到了一个方法,但我认为效率很低。
以下代码从单词表单元格中获取文本,并用它的颜色打印每个单词。
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
您知道任何其他(更好)的方法来实现这一目标吗?
谢谢。
这是一种从 Word 文档中提取突出显示的单词的方法python-docx:
#!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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)