2017-10-14 104 views
0

我在表中計算行值的時候遇到了問題。這裏是我關於這個問題的細節,我有一個由多個表組成的docx文件。我需要計算具有「黑盒測試」的Total「Test Type」行的值。我已經問過這個問題,但仍然無法執行。在那個docx中有很多種表格和段落。所以我很困惑,如何檢索特定的表並計算行的值。該表將是這樣的First i need to retrieve the Test Case Detail and then I need to count the total no of black box testing. For example my docx consist nearly 300 above tables so It is hard to count the row's values. please help me如何使用python計數docx文件中表中行的值

我使用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由上面的表格組成,因此很難對行的值進行計數。請幫幫我。

在此先感謝!

回答

1

試試這個:

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) 

輸入是:

enter image description here

+0

它只能給予零黑盒測試。 –

+0

我沒有得到答案。 –

+0

我得到一個輸出 - 黑盒測試總數爲:2.檢查您測試的docx行中的文本是否具有確切的外框:'測試類型'和'黑盒子'。 – skrubber

相關問題