Python to mailmerge csv to word in continuous in one document

17 Views Asked by At

I want to mailmerge a table in csv(List.csv) to word(example1.docx) but continuously into multiple pages in one word document. The function is something like "Next Record" in MS Word. I have a template (Test.docx). I can't find in Python.

from __future__ import print_function
import pandas as pd
from mailmerge import MailMerge
from datetime import date

# Define the templates - assumes they are in the same directory as the code
template_1 = "Test.docx"
df1 = pd.read_csv('List.csv')
looprange = range(int(len(df1.index)))

for j in looprange:
    document_1 = MailMerge(template_1)
    document_1.merge(
        BusinessName=df1.BusinessName[j],
        Name= df1.Name[j],
       )
document_1.write('example1.docx')

List.csv

enter image description here

Test.docx

enter image description here

However, I only get the output of the last input and is only one page and it is repeated data. Desired output suppose to be suppose to be all 4 row data in two pages. The data should be different left and right as I split the template to two column.

Output currently:

enter image description here

0

There are 0 best solutions below