如何从 .docx 文件中的 table 中提取文本?

How to extract text from a table in a .docx file?

我想使用 python 从 .docx 文件中的 table 中提取文本以供进一步分析。我正在使用以下代码:

document = Document(path_to_your_docx)
tables = document.tables
for table in tables:
    for row in table.rows:
        for cell in row.cells:
            for paragraph in cell.paragraphs:
                print(paragraph.text)

但是这个 table 的单元格中似乎还有另一个 "table",所以我无法提取这部分(如附图所示)。当我使用上面的代码时,我无法获取 "Yes/No" 文本。

我也试过像在 table 中一样遍历单元格,但我收到单元格没有 table 属性的错误。有什么建议吗?

The table looks like this

code behind table creation

谢谢。

我有解决此问题的方法。我没有使用库 python-docx 从 docx 文件中提取文本,而是使用库 docx2txt (提取 all 文本)然后我只需要在字符串中找到特定的单词。

text = docx2txt.process(file)

q = "Example1"
result = text[text.find(q)+len(q):].split()[0]

这为我提供了 Column2 中的 "Yes" 或 "No",对于 Column1 上的每个值(在上面的示例中,它给出 Yes)。