Python Forum
Need help with For Loop logic for reading docx Table column data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with For Loop logic for reading docx Table column data
#1
Hi Everyone,

Please help with the loop logic.

There are multiple tables available in the word docx.
I am trying to read a particular Table e.g. Table2 in a docx document.
I need to read contents only from column 3 of Table2 having column name as 'status'.

The current logic, it reads all the rows data from Table 2 till Table 3. How do I make read only column3 data from Table2.


Table1
Sr. Name Age City
1 Mr. XYZ 12 sfs
2 Mr. YDS 34 safdas
3 Mr. ZSD 20 asfd
4 Mr. BSD 50 asd

Table2
Sr. Name Status City
1 Mr. XYZ Single sfs
2 Mr. YDS Married safdas
3 Mr. ZSD Single asfd
4 Mr. BSD Divorcee asd

Table3
Sr. Name Age City State
1 Mr. XYZ 12 sfs MK
2 Mr. YDS 34 safdas JS
3 Mr. ZSD 20 asfd SD
4 Mr. BSD 50 asd WE


import docx
file2 = docx.Document("C:\\Test_Document.docx")
document1 = file2
columns1=[]
i = 0
j = 0

def function_verifystatus(document1):
    flag1 = True
    
    for (table_no,table) in enumerate(document1.tables):
        
        for (i,row) in enumerate(table.rows):
            for (j,cell) in enumerate(row.cells):
                if cell.text == "Status":
                    k = j
                    flag1 = False            
            if (flag1 == False):
                for (i,k) in enumerate(row.cells):
                    columns1.append(k.text)
                    print("status value is:", k.text)
    flag1 = True
        
        
    return columns1
        
result1=function_verifystatus(document1)
Reply
#2
You may get an answer to your question here, but perhaps you should contact the author of the package here: [email protected]
Reply
#3
Thanks Larz. I checked that google group. It seems, it has not been active for a while now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  no module named 'docx' when importing docx MaartenRo 1 852 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Returning Column and Row Data From Spreadsheet knight2000 0 435 Oct-22-2023, 07:07 AM
Last Post: knight2000
  Using pyodbc&pandas to load a Table data to df tester_V 3 802 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Find a string from a column of one table in another table visedwings049 8 1,169 Sep-07-2023, 03:22 PM
Last Post: deanhystad
  Database that can compress a column, or all data, automatically? Calab 3 1,164 May-22-2023, 03:25 AM
Last Post: Calab
  Code for pullng all data in a column EmBeck87 5 1,103 Apr-03-2023, 03:43 PM
Last Post: deanhystad
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,032 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  reading a table which is of type string saisankalpj 2 957 Dec-03-2022, 11:19 AM
Last Post: saisankalpj
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,341 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Reading Data from JSON tpolim008 2 1,077 Sep-27-2022, 06:34 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020