-1

My script for counting tables and rows in the word docx working well. My question is why my printing statement printing till the condition gets fail. I attached the image for your reference

enter image description here

Here is my code:

from docx import Document
doc = Document("sample2.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" or ro.cells[2].text=="Black-Box"):
            i=i+1
print("Total Black Box Tests are: ", i)

j=0
for table in doc.tables:
    for row in table.rows:
        if row.cells[0].text=="ID":
            j=j+1
            print("Total no of Tables:", j)

Help me. Thanks!

Aroon
  • 991
  • 2
  • 15
  • 34

1 Answers1

1

Problem with indentation of last print statement Try this

from docx import Document
doc = Document("sample2.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" or ro.cells[2].text=="Black-Box"):
            i=i+1
print("Total Black Box Tests are: ", i)

j=0
for table in doc.tables:
    for row in table.rows:
        if row.cells[0].text=="ID":
            j=j+1
print("Total no of Tables:", j)
Sandeep Lade
  • 1,865
  • 2
  • 13
  • 23