I have a word file with MergeFields key1,key2,key3..... I want to prefill this word document with values1, values2, values3.... at each MergeField respectively. If I write out the values explicitly like so, it works perfectly fine
with MailMerge(file_path_with_old_name) as document:
document.merge(
key1 = value1,
key2 = value2,
key3 = value3
....)
document.write(file_path_with_new_name)
but I would like to be able to pass a dictionary to the MailMerge instead of having to write it out. the keys would be the word document MergeFields and the values would be what replaces them ie key1 is replaced by value1 :
a = { key1:value1, key2:value2, key3:value3.....}
with MailMerge(file_path_with_old_name) as document:
document.merge(a)
document.write(file_path_with_new_name)
I found this questions (How do I get a python dictionary into a document merge as kwargs with docx-mailmerge) that seems to ask the same thing I am asking, they suggested to place ** before the dictionary but when I do that, I get the following error
File "C:/Python_All/python_scripts/data_gather_2.0.py", line 151, in template_manipulation
document.merge(**all_fields_full) # merges documents note that the ** is critical it unpacks the dictionary
File "C:\Python_All\Anaconda\lib\site-packages\mailmerge.py", line 179, in merge
self.__merge_field(part, field, replacement)
File "C:\Python_All\Anaconda\lib\site-packages\mailmerge.py", line 191, in __merge_field
text_parts = text.replace('\r', '').split('\n')
AttributeError: 'tuple' object has no attribute 'replace'