-2

I want to create a table using docxtpl in python, where the table rows should be dynamic

For eg:

user_dict = [{"name": "Person 1", "age": 25, "job": "Software Engineer"},
         {"name": "Person 1", "age": 25, "job": "Software Engineer"},
         {"name": "Person 1", "age": 25, "job": "Software Engineer"},
         {"name": "Person 1", "age": 25, "job": "Software Engineer"}]

if the length of list is n table should dynamically populate

0

1 Answer 1

0

I got the output after I was going through the documentation of Docxtpl and tried using list of dictionary

To create a table with Docxtpl in python

Sample jinja template format

    from docxtpl import DocxTemplate

user_dict = [{"name": "Person 1", "age": 25, "job": "Software Engineer"},
             {"name": "Person 1", "age": 25, "job": "Software Engineer"},
             {"name": "Person 1", "age": 25, "job": "Software Engineer"},
             {"name": "Person 1", "age": 25, "job": "Software Engineer"}]

doc = DocxTemplate("dynamic_table_tpl.docx")
context = {'user_record': user_dict}
context.update(context)
doc.render(context)
doc.save(f"user.docx")

Out of the above program

#python #docxtpl

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.