使用 python 计算 word docx 中的行数及其值

To count the rows and its values in word docx by using python

我有一个不包含 table 的单词 docx。每个 table 都有不同的行和列名称,但在所有不同的 table 中,所有行名称都是相同的,即 "test automation",它的值为 "yes or no" 。我的问题是如何计算像这样的 "test automation" 行值的总数 "TOTAL NO OF TEST AUTOMATION:yes=200,no=100" 我正在使用 python 3.6。我是 python 的新手,请帮助我。 table 提取和特定列提取的示例代码。

示例数据图片:示例数据

我的代码看起来像这样来提取 docx table

import pandas as pd
from docx.api import Document

document = Document('test_word.docx')
table = document.tables[0]

data = []

keys = None
for i, row in enumerate(table.rows):
    text = (cell.text for cell in row.cells)

    if i == 0:
        keys = tuple(text)
        continue
    row_data = dict(zip(keys, text))
    data.append(row_data)
    print (data)

df = pd.DataFrame(data)
print(df)

这是您需要为测试自动化计算“是”值的基本逻辑。您需要处理您需要的任何 Pandas 操作:

from docx import Document

def table_test_automation(table):
    for row in table.rows:
        row_heading = row.cells[0].text
        if row_heading != 'Test automation':
            continue
        yes_no = row.cells[3].text
        return 1 if yes_no == 'Yes' else 0

    return 0


document = Document('test_word.docx')
yes_count = 0
for table in document.tables:
    yes_count += table_test_automation(table)
print(yes_count)