问题
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