Style for word table Python-docx

1

I am trying to generate a word document with Python, for this I use the python-docx module. I want to establish a very specific style for a table in the word document: 'Grid Table 5 Dark Accent 1'. But this particular style is not found in the list of available styles.

from docx import Document

document = Document()
table = document.add_table(rows=1, cols=3, style='GridTable5Dark-Accent1')

Give an error of the following type:

table = document.add_table(rows=1, cols=3, style='GridTable5Dark-Accent1')
File "/usr/local/lib/python3.5/dist-packages/docx/document.py", line 100, in add_table
table.style = style
File "/usr/local/lib/python3.5/dist-packages/docx/table.py", line 134, in style
style_or_name, WD_STYLE_TYPE.TABLE
File "/usr/local/lib/python3.5/dist-packages/docx/parts/document.py", line 76, in get_style_id
return self.styles.get_style_id(style_or_name, style_type)
File "/usr/local/lib/python3.5/dist-packages/docx/styles/styles.py", line 113, in get_style_id
return self._get_style_id_from_name(style_or_name, style_type)
File "/usr/local/lib/python3.5/dist-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 "/usr/local/lib/python3.5/dist-packages/docx/styles/styles.py", line 57, in __getitem__
raise KeyError("no style with name '%s'" % key)
KeyError: "no style with name 'GridTable5Dark-Accent1'"

I have another document (handwritten) where I do have the style that I want, I thought that with this doc with the style that I want, I may be able to extract it and apply it to my own document. But I can not find a way to achieve it ...

I guess something has to do with the documentation where it explains the styles and like play with them but I do not know if I do not find out or the documentation is not It's clear enough, but I can not fix it ...

I tried the following:

document = Document()
doctemplate = Document('SG.docx')

# Cogemos el estilo que yo quiero
styleIneed = doctemplate.styles['Grid Table 5 Dark Accent 1']
# Añadimos un estilo vacío
newstyle = document.styles.add_style('Grid Table 5 Dark-Accent 1', WD_STYLE_TYPE.TABLE)
# Especificamos qué debe llevar el estilo nuevo creado
newstyle.base_style = styleIneed
# Creo la tabla
table = document.add_table(rows=3, cols=3, style='GridTable5Dark-Accent1')

But it does not work, the table is created but without any style ... Would someone know how to do it or give me some guidelines for it?

Thank you very much in advance !!

    
asked by Ergato 17.07.2018 в 11:03
source

1 answer

-1

table = doc.add_table (rows = 9, cols = 2, style = 'TableGrid') the style of table is called "Table Grid" I suppose you should only put without spaces the style that you want

    
answered by 26.07.2018 в 01:24