Bouke / docx-mailmerge

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.
MIT License
274 stars 104 forks source link

document.merge() in for-loop #62

Closed KRitter90 closed 5 years ago

KRitter90 commented 5 years ago

The idea: I want to have a list of objects, that will be the input values for the document.merge() function

The test version:

document = MailMerge(template)
testDoc=document.get_merge_fields()
textMergeFields=list(testDoc)

# transform values to strings
ValuesString=[str(i) for i in Values]

# check if same length
if len(textMergeFields) == len(ValuesString):

    # insert values into the word template
    counter=0
    for i in textMergeFields:
        document.merge( i = ValuesString[counter])
        counter=counter+1

    # write into the document and save it
    template_out='C:\\Users\\dummy.docx'
    document.write(template_out)

The issue: I don't get an error - but I also can't see any inserted values in the MergeFields of my Word .docx template.

The question: Is there any chance to fix this?

giguerefrancoisx commented 5 years ago

You shouldn't pass parameters to document.Merge() one at a time. Instead, build a dictionary and pass that as your parameter.

my_parameters = {'One': 'One', 'Two': 'Two'}
document.merge(**my_parameters)

which is equivalent to:

document.merge(One = 'One', Two = 'Two')