4
votes

I'm trying to create a word document using docx module of Python. However I am unable to add table border to it.

My code is as below:

    import docx
    from docx import Document
    from docx.shared import Pt
    doc = Document('C:/Users/Vinny/Desktop/Python/Template.docx')
    doc.add_paragraph('Changes:')

    doc.add_paragraph('Metrics:')
    #add table
    table = doc.add_table(rows = 4, cols = 2, style='TableGrid')
    doc.save('C:/Users/Vinny/Desktop/Python/rel.docx')

But it throws error as:

Traceback (most recent call last):
  File "C:\Users\Vinny\Desktop\Python\abc.py", line 14, in <module>
    table = doc.add_table(rows = 4, cols = 2, style='TableGrid')
  File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\document.py", line 100, in add_table
    table.style = style
  File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\table.py", line 134, in style
    style_or_name, WD_STYLE_TYPE.TABLE
  File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\parts\document.py", line 76, in get_style_id
    return self.styles.get_style_id(style_or_name, style_type)
  File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\styles\styles.py", line 113, in get_style_id
    return self._get_style_id_from_name(style_or_name, style_type)
  File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\styles\styles.py", line 143, in _get_style_id_from_name
    return self._get_style_id_from_style(self[style_name], style_type)
  File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\styles\styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
KeyError: "no style with name 'TableGrid'"

Can anyone help me out with this?

3
Make sure you read this page in the documentation and the one immediately following it (using the Next button): python-docx.readthedocs.io/en/latest/user/… - scanny
It worked fine and created the desired document. I added table grid to the template, deleted and saved it. Works fine. Only thing is displays a warning message every time. Warning message goes like: " File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\styles\styles.py", line 54 warn(msg, UserWarning) UserWarning: style lookup by style_id is deprecated. Use style name as key instead.s : " - Marvin
What's the rest of the stack trace? - scanny
@VinnyKaur Check this link to fix your warning stackoverflow.com/questions/28973277/… - Maruti Mohanty
ah ! I checked that, and it worked, Thank you @MarutiMohanty . - Marvin

3 Answers

6
votes

Try with a space:

table = doc.add_table(rows = 4, cols = 2, style='Table Grid')

I had the same problem and this worked for me.

0
votes
from docx.enum.style import WD_STYLE_TYPE

also add the space to make style='Table Grid'

0
votes

Using space:

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