如何使用 python 计算 docx 文件中表中行的值

How to count the row's values in tables in docx file by using python

我在计算 table 中的行值时遇到问题。这是我关于这个问题的详细信息,我有一个包含许多 table 的 word docx 文件。我需要计算具有 "black box testing" 的总 "Test Type" 行的值。我已经问过这个问题,但仍然无法执行。在那个docx中有很多种tables和段落。所以我很困惑如何检索特定的 tables 并计算行的值。 table 会像这样

我正在使用 python 3.6,我也安装了 docx 模块。

我试过的代码:

from docx import Document

def table_test_automation(table):
    for row in table.rows:
        row_heading = row.cells[9].text
        if row_heading != 'Test Type':
            continue
        Black_box = row.cells[1].text
        return 1 if Black_box == 'Black Box' else 0

    return 0


document = Document('VRRPv3-PEGASUS.docx')
yes_count = 0
for table in document.tables:
    yes_count += table_test_automation(table)
print("Total No Of Black_box:",yes_count)

首先我需要检索测试用例详细信息,然后我需要计算黑盒测试的总数。例如,我的 docx 在 tables 以上包含近 300 个,因此很难计算行的值。请帮助我。

提前致谢!

试试这个:

doc = Document("sample.docx")
i = 0 
for t in doc.tables:
    for ro in t.rows:
        if ro.cells[0].text=="Test Type" and ro.cells[2].text=="Black Box":
            i=i+1
print("Total Black Box Tests are: ", i)

输入为: