2

I want to add multiple tables in MS word using python. I am trying the following code:

i=0
    while i < 9: 
     f=open("file", "a+")
     document = Document()
     table[i] = document.add_table(rows=5, cols=4)
     i=i+1
     document.save('file')

When I open the document, however, there is only one table.

1
  • What python module are you using to do your word doc creation? Commented Oct 30, 2019 at 22:44

3 Answers 3

2

The Document object should be created and saved outside the loop so that multiple tables can be added to it:

document = Document()
i = 0
while i < 9:
    table[i] = document.add_table(rows=5, cols=4)
    i = i + 1
document.save('file')
Sign up to request clarification or add additional context in comments.

Comments

0

I tried this solution and it only add more rows and columns to the same table

Comments

0

I realize that this question is old, but the solution is simply to add a paragraph between each table. If there is nothing separating the tables, they are joined.

document = Document()
for i in range(9):
    document.add_table(rows=5, cols=4)
    document.add_paragraph('')
document.save('file')

Comments

Your Answer

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