2

I have been trying to figure out how to add cell 0 from all tables in a word document to my_list , I managed to add from a specific table placement number (96) but can't seem to pull all tables data, I used this for table array number 96 and it worked

`tables = list(d.tables)
tbl = d.tables[96]
my_list = []
for rw in tbl.rows:
    my_list.append(rw.cells[0].text)

print(my_list)

tried a lot of different options to iterate all tables and add to my_list, one being below but it gives the error 'Table' object has no attribute 'cells'

my_list = []

tbl = list(d.tables)
for val in tbl:
    for rw in tbl:
        my_list.append(rw.cells[0].text)


print(my_list)

any help is greatly appriciated

1 Answer 1

1

This is not a complete runnable example so I cannot verify it, but:

This part is Bad Python:

for val in tbl:
    for rw in tbl:

You are iterating over tbl twice, and all the outer loop is doing is running the inner loop len(tbl) times. The second line might be

for rw in val:

but I am not 100% on that due to Not Enough Information.

(Also, consider renaming both tbl and val to something resembling what they contain: list_of_tables and a_table would be far better. Also, use row instead of rw, as Python does not have some unreasonable variable name length width.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your recommendation, it led me down the right path, i just added .rows and it worked. for table in tables: for row in table.rows:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.