我知道也有类似的问题,但我找不到能回答我祈祷的东西。我需要的是一种从 MS-Word 文件访问某些数据并将其保存在 XML 文件中的方法。 阅读python-docx并没有帮助,因为它似乎只允许人们写入word文档,而不是阅读。 为了准确地呈现我的任务(或者我选择如何完成我的任务):我想在文档中搜索关键字或短语(文档包含表格)并从关键字/短语所在的表格中提取文本数据成立。 有人有什么想法吗?
docx 是一个包含文档 XML 的 zip 文件。您可以打开 zip、阅读文档并使用 ElementTree 解析数据。
这种技术的优点是你不需要安装任何额外的Python库。
import zipfile
import xml.etree.ElementTree
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
TABLE = WORD_NAMESPACE + 'tbl'
ROW = WORD_NAMESPACE + 'tr'
CELL = WORD_NAMESPACE + 'tc'
with zipfile.ZipFile('<path to docx file>') as docx:
tree = xml.etree.ElementTree.XML(docx.read('word/document.xml'))
for table in tree.iter(TABLE):
for row in table.iter(ROW):
for cell in row.iter(CELL):
print ''.join(node.text for node in cell.iter(TEXT))
请参阅我在 stackoverflow 上的回答 如何使用 Python 读取 MS-Word 文件中的表格内容?以获取更多详细信息和参考。
在回答下面的评论时, 图像提取起来并不那么清晰。我创建了一个空的 docx 并向其中插入了一张图像。然后,我以 zip 存档形式打开 docx 文件(使用 7zip)并查看 document.xml。所有图像信息都作为属性存储在 XML 中,而不是像文本那样存储在 CDATA 中。所以你需要找到你感兴趣的标签并拉出你正在寻找的信息。
例如添加到上面的脚本中:
IMAGE = '{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}' + 'docPr'
for image in tree.iter(IMAGE):
print image.attrib
输出:
{'id': '1', 'name': 'Picture 1'}
我不是 openxml 格式方面的专家,但我希望这会有所帮助。
我确实注意到 zip 文件包含一个名为 media 的目录,其中包含一个名为 image1.jpeg 的文件,其中包含嵌入图像的重命名副本。您可以查看 docx zip 文件以调查可用的内容。
使用 python-docx 在文档中搜索
# Import the module
from docx import *
# Open the .docx file
document = opendocx('A document.docx')
# Search returns true if found
search(document,'your search string')
您还有一个获取文档文本的函数:
https://github.com/mikemaccana/python-docx/blob/master/docx.py#L910
# Import the module
from docx import *
# Open the .docx file
document = opendocx('A document.docx')
fullText=getdocumenttext(document)
使用python从doc/docx文件中提取文本
import os
import docx2txt
from win32com import client as wc
def extract_text_from_docx(path):
temp = docx2txt.process(path)
text = [line.replace('\t', ' ') for line in temp.split('\n') if line]
final_text = ' '.join(text)
return final_text
def extract_text_from_doc(doc_path):
w = wc.Dispatch('Word.Application')
doc = w.Documents.Open(file_path)
doc.SaveAs(save_file_name, 16)
doc.Close()
w.Quit()
joinedPath = os.path.join(root_path,save_file_name)
text = extract_text_from_docx(joinedPath)
return text
def extract_text(file_path, extension):
text = ''
if extension == '.docx':
text = extract_text_from_docx(file_path)
else extension == '.doc':
text = extract_text_from_doc(file_path)
return text
file_path = #file_path with doc/docx file
root_path = #file_path where the doc downloaded
save_file_name = "Final2_text_docx.docx"
final_text = extract_text(file_path, extension)
print(final_text)
看来 pywin32 可以解决这个问题。您可以遍历文档中的所有表格以及表格内的所有单元格。获取数据有点棘手(必须省略每个条目的最后 2 个字符),但除此之外,这是一个十分钟的代码。 如果有人需要更多详细信息,请在评论中说明。
一个更简单的具有图像提取功能的库。
pip install docx2txt
然后使用下面的代码来读取docx文件。
import docx2txt
text = docx2txt.process("file.docx")
您可以使用Aspose.Words阅读文档。当文档加载到 Aspose.Words 中时,
Document
对象表示为 DOM,您可以通过编程方式读取它。
例如,以下代码读取文档中的第一个表格并检查每行的第一个单元格作为键:
import aspose.words as aw
key = "Name"
doc = aw.Document("C:\\Temp\\in.docx")
table = doc.first_section.body.tables[0]
for r in table.rows :
row = r.as_row()
first_cell_text = row.first_cell.to_string(aw.SaveFormat.TEXT).strip()
if first_cell_text == key :
print(row.cells[1].to_string(aw.SaveFormat.TEXT).strip())