for loop and same context (doxctpl)

Viewed 11

I have made a code to generate the word document report and even with the for loop I end up getting multiple documents only difference in the image, meaning all other data such as title and volume and rate and price are same for all documents.

I used doxctpl , Docxtemplate for coding, I created the template word doc with image and words. then, I tried to change the words context first then change images in the coding.

for i in csv:  #csv file has multiple columns named title, volume, rate, price info)
    number = number + 1
    DEST_FILE = "dir/auto_" + str(number) +  ".docx"   # to save individual doc file
    Title = products[0].product_title
    Volume = products[0].lastest_volume
    Rate = products[0].evaluate_rate
    Price = products[0].sale_price
    context = {"Title": Title, "Volume": Volume, "Rate": Rate, "Price": Price}
    print(context)
    for file in files:
        old_im = 'dir.media_to_paste.jpg'
        new_im = f"image/{file}"
        tpl.replace_media(old_im, new_im)
    tpl.render(context)
    tpl.save(DEST_FILE)

I changed the image to change first, but the result are same.

Results show as

auto1.docx
Image1 + Title 1, Volume 1....

auto2. docx
Image2 + Tilte 1, Volume 1 ....

1 Answers
for i in op[1:]:
    number = number + 1
    Title = i.split(",")[0]
    Volume =i.split(",")[1]
    Rate = i.split(",")[2]
    Price = i.split(",")[3]
    Currency = i.split(",")[4]
    old_im = 'dir.media_to_paste.jpg'
    new_im = f"image/{file}"
    tpl.replace_media(old_im, new_im)
    doc.render(context)

fixed

Related