Search code examples
datatablestylesdocxpython-docxpython-3.10

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

  • 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')