To count the row's values in different tables which are presented in docx by python

梦想与她 提交于 2019-12-12 05:28:26

问题


I have a word docx which consist lots of table. so I'm getting trouble to go through all the table and counting some details. I need to automate those cases. Here my question is, First thing i need to read the table which has the header of "Test case details" then i need to count the "Test Type" row which has the "black box" testing value. Here i attached the word docx image for your concern. I need the output like "Total no of Black box test: 200". I'm using python 3.6, Please help me.

sample image of docx

sample code, i tried

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)

回答1:


It's not clear what the contents of the first row is, so this might take some experimentation.

The place to start is by printing out the contents of the table heading cell:

table_heading_text = table.rows[0].cells[0].text
print(table_heading_text)

If that text were "Test Case Details", you can just test on that to qualify the table for further processing.

The thing that makes me suspicious is the disk icon. If that cell contains only an image, this approach isn't going to work.



来源:https://stackoverflow.com/questions/46723873/to-count-the-rows-values-in-different-tables-which-are-presented-in-docx-by-pyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!