0

I have a word document page.docx containing tables and the python file file 1.py:

from docxtpl import DocxTemplate

doc = DocxTemplate("page.docx")
context = { 'text': 'there could be your ad'}

a = doc.tables

for i in a:
    if i.cell(0,0).text == 'some_text_in_title':
        print(i.cell(0,0).text)
        i.add_row()

doc.render(context)
doc.save("page1.docx")

This inserts new rows into the file but doesn't add borders to these new rows.

How can I set border settings in docxtpl?

3
  • try with i.style = 'Table Grid' Commented Jul 16, 2019 at 11:46
  • KeyError: "no style with name 'Table Grid'" Commented Jul 16, 2019 at 12:00
  • Also, ``` for style in doc.styles: print(style.name) ``` Gives that result ``` Normal Default Paragraph Font Normal Table No List ``` Commented Jul 16, 2019 at 12:53

1 Answer 1

1

The python-docx module is more suited for your purpose of modifying an existing table in a Word document.

from docx import Document

document = Document(input_document_path)
tables = document.tables

for table in tables:
    for row in table.rows:
        if row.cells[0].text=="text_to_match":
            print(row.cells[0].text)
            table.add_row().cells

document.save(output_file_path)

The rows newly created with the add_row() function retain the style attributes of the original table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.