python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.59k stars 1.13k forks source link

table style KeyError: u"no style with name 'Table Grid'" #504

Open robinshion opened 6 years ago

robinshion commented 6 years ago

I trying to insert a table and Grid the frame

def buildtable():

    with open(idcsvFilePathLeft,'rb') as idcsv:
        idlist= csv.reader(idcsv)
        idlist=list(idlist)

    with open(logcsvFilePathLeft,'rb') as logcsvfile:
        loglist= csv.reader(logcsvfile)
        loglist=list(loglist)

    id_numbers=sum(1 for row in idlist)
    table=doc.add_table(rows=9,cols=5,style = 'Table Grid')
    table.alignment = WD_TABLE_ALIGNMENT.CENTER

    table.cell(0,0).text="Name"
    table.cell(0,0).paragraphs[0].runs[0].font.size=Pt(20)
    table.cell(0,0).paragraphs[0].runs[0].bold=True
    table.cell(0,0).paragraphs[0].alignment=WD_ALIGN_PARAGRAPH.CENTER

buildtable()    

but I get error is said

Traceback (most recent call last): File "C:\Users\Robin-work-laptop\Dropbox\dot entrance log\PCsite\makereport.py", line 79, in buildtable() File "C:\Users\Robin-work-laptop\Dropbox\dot entrance log\PCsite\makereport.py", line 65, in buildtable table=doc.add_table(rows=9,cols=5,style = 'Table Grid') File "C:\Python27\lib\site-packages\docx\document.py", line 100, in add_table table.style = style File "C:\Python27\lib\site-packages\docx\table.py", line 134, in style style_or_name, WD_STYLE_TYPE.TABLE File "C:\Python27\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:\Python27\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:\Python27\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:\Python27\lib\site-packages\docx\styles\styles.py", line 57, in getitem raise KeyError("no style with name '%s'" % key) KeyError: u"no style with name 'Table Grid'"

please help

Benjamin-T commented 6 years ago

Is the 'Table Grid' style actually defined in the template you are using? Otherwise, create your own template, and add the table to this file,

table_doc = Document("custom_template_containing_TableGrid_style.docx")
table_doc.add_table(rows=9,cols=5,style = 'Table Grid')
robinshion commented 6 years ago

Thanks @Benjamin-T I do the same as you said but not working still get the error

Benjamin-T commented 6 years ago

Do you have a template / *.docx file which contains a table style called "Table Grid"?

robinshion commented 6 years ago

I created .docx file but I didn't add table in. I trying to using python to add table in the docx file.

Benjamin-T commented 6 years ago

look into the difference between a Table and a TableStyle

https://support.office.com/en-us/article/apply-table-styles-0f19e350-f9a1-4a9f-afdb-46ee2bb8460c

robinshion commented 6 years ago

I read the word actually has the "Table Grid " table style. Be honestly my code is work long time before just stop working yesterday. So weird.....

Benjamin-T commented 6 years ago

Can you share the docx template?

robinshion commented 6 years ago

logsample.docx

robinshion commented 6 years ago

That is my template using

Benjamin-T commented 6 years ago

It seems that you do not use the template in your code above. I made a minor change, it now actually uses your template file.

regards, Ben

def buildtable(doc):
    with open(idcsvFilePathLeft,'rb') as idcsv:
        idlist= csv.reader(idcsv)
        idlist=list(idlist)

    with open(logcsvFilePathLeft,'rb') as logcsvfile:
        loglist= csv.reader(logcsvfile)
        loglist=list(loglist)

    id_numbers=sum(1 for row in idlist)

    table=doc.add_table(rows=9,cols=5,style = 'Table Grid')
    table.alignment = WD_TABLE_ALIGNMENT.CENTER

    table.cell(0,0).text="Name"
    table.cell(0,0).paragraphs[0].runs[0].font.size=Pt(20)
    table.cell(0,0).paragraphs[0].runs[0].bold=True
    table.cell(0,0).paragraphs[0].alignment=WD_ALIGN_PARAGRAPH.CENTER
doc = Document('logsample.docx')            
buildtable(doc)
robinshion commented 6 years ago

Thanks for your code . I tried but get same error. but I found that if I put one table already in my template.docx it will be fine but if my template.docx doesn't have table yet,style="Table Grid" will be error

scanny commented 6 years ago

Put one in then take it out and save that file. The table goes, but the style stays behind.

Be sure you read and understand the documentation on styles to understand their subtleties:
http://python-docx.readthedocs.io/en/latest/user/styles-understanding.html
http://python-docx.readthedocs.io/en/latest/user/styles-using.html

hamodey commented 1 year ago

@scanny Thanks for that! The issue seems to come from the template.

If you do not use a template, and generate a new word document - then all styling referenced here works just fine.

However, once you apply a template on the generation of document all of that goes away. So, as scanny suggested above, a great solution is to open the template, create and save all table or styles you prefer - then delete them.

Once you create a new word document this should all work fine.

Singaram-117 commented 8 months ago

thx, I was tired of trying out other solutions but finally found that you have to create the table once with the 'Table Grid' style in the Word document manually and delete it from the template.docx file. So that Python can edit the document files without any error.