Open robinshion opened 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')
Thanks @Benjamin-T I do the same as you said but not working still get the error
Do you have a template / *.docx file which contains a table style called "Table Grid"?
I created .docx file but I didn't add table in. I trying to using python to add table in the docx file.
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
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.....
Can you share the docx template?
That is my template using
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)
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
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
@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.
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.
I trying to insert a table and Grid the frame
but I get error is said
please help