4

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?

Tatsuyuki Ishi
  • 3,883
  • 3
  • 29
  • 41
Marvin
  • 421
  • 1
  • 5
  • 15
  • 1
    Make sure you read this page in the documentation and the one immediately following it (using the Next button): http://python-docx.readthedocs.io/en/latest/user/styles-understanding.html – scanny Apr 23 '17 at 07:52
  • 1
    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 Apr 25 '17 at 03:46
  • What's the rest of the stack trace? – scanny Apr 25 '17 at 14:24
  • 1
    @VinnyKaur Check this link to fix your warning http://stackoverflow.com/questions/28973277/python-docx-style-id-error-while-creating-a-word-document – Maruti Mohanty Apr 25 '17 at 17:22
  • ah ! I checked that, and it worked, Thank you @MarutiMohanty . – Marvin Apr 25 '17 at 17:27
  • Does this answer your question? [Specify border appearance in tables using python-docx](https://stackoverflow.com/questions/50685458/specify-border-appearance-in-tables-using-python-docx) – JustCarty Jul 02 '23 at 11:49

3 Answers3

8

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.

ncica
  • 7,015
  • 1
  • 15
  • 37
Diego
  • 1,232
  • 17
  • 20
0
from docx.enum.style import WD_STYLE_TYPE

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

ncica
  • 7,015
  • 1
  • 15
  • 37
  • @AhmedMamdouh Is there a reason you think that it doesn't work? It definitely works for me as well for a few others that have responded to this question. – Zenon Anderson Dec 15 '20 at 00:50
0

Using space:

table = document.add_table(rows=1, cols=3, style='Table Grid')
ncica
  • 7,015
  • 1
  • 15
  • 37
Sergei
  • 9
  • 1