如何从python中许多word文档的多个表中提取所有数据(直接从MS Word中提取数据)?

时间:2018-10-07 15:17:02

标签: python ms-word data-extraction

我尝试使用以下代码,但是它只能打开一个文档以打印单元格文本。

问题是我有67个单词文档,它们具有相似的表格,如何从每个67个单词文档中的表格中提取所有数据?

当前,以下代码只能打开一个文档以提取所有表中的单元格文本,但是,我想使用以下代码在一个文件夹中打开多个Word文档。 因此,是否可以使用以下代码打开多个Word文档?  请帮忙看看下面的代码,谢谢!!! :((

from docx import Documenthttps

wordDoc = Document(r"C:\Users\user\Documents\Lynn\FYPJ P3\FYP (Updated Ver)\FYP\dataprep\documents_sampling\860305644_Cat_5_Patient Care Record (Inpatient Nursing)_Admission.docx")
for table in wordDoc.tables:
    for row in table.rows:
        for cell in row.cells:
            print(cell.text)

1 个答案:

答案 0 :(得分:1)

您可以只使用它:

import os
from docx import Documenthttps

path = '\\some\\path\\to\\folder'
worddocs_list = []
for filename in os.listdir(path):
    wordDoc = Document(path+"\\"+filename)
    worddocs_list.append(wordDoc)

for wordDoc in worddocs_list:
    for table in wordDoc.tables:
        for row in table.rows:
            for cell in row.cells:
                print(cell.text)