0

I want to create a docx report with the information that I get from an excel. Lets say I have an excel like this

Name Biography Age john He is a doctor. 30 michael he is an actor. 31 Lary He is a student. 22

My world should look like

[Some cover]

name: john

Biography:He is a doctor.

Age: 30


name:Michael

Biography:He is an actor

Age:31


name:Lary

Biography: He is a student

Age:22


Basically I have used the similar code within this question. But the difference is that I would like to collect all them in the same docx.

My codes output is like:

name:Lary

Biography: He is a student

Age:22

Because it overwrites the template into my output word.

My question is that how can I append every person instead of overwriting every time?

mozway
  • 194,879
  • 13
  • 39
  • 75

1 Answers1

1

First, create a nested dictionary with your data:

people_dict = {'John':{"Age":29, "Bio": "Doctor"}, 
               'Michael':{"Age":34, "Bio": "Actor"},
               'Larry':{"Age":45, "Bio": "Student"}}

Then, in your template create a for loop to iterate over people_dict: screenshot of template for loop

Next, create the context variable with context = {'people_dict':people_dict}.

Render the context and the output will be: output of script

I figured this out by referencing this Jinja2 documentation on loops and conditionals

p_sutherland
  • 471
  • 1
  • 11
  • 21