'python-docx table styles not accepting styles

I am trying to make a table in Word with python-docx, but after creating the table and saving the file, the table doesn't have any lines / separators. So I tried to use the table.style option, but I just can't get any style to work, except Normal Table (which is the default).

This is the code I use to create the table:

import docx

file = docx.Document()
table = file.add_table(6, 4)

fRow = table.rows[0]
fRow[0].text = "some headline"
... 
table.style = "<stylename here>" 

file.save("test.docx")

All of the styles I tried are from this website: https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html#built-in-styles

I am using Python 3.10.0b4 on Windows 11.



Solution 1:[1]

This is the code I used to accomplish this:

table = document.add_table(rows=1, cols=2, style='Table Grid')

for your line, believe it would be

table = file.add_table(6, 4, style='Table Grid')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SlowLearner