Mail Merge from DataFrame into Word Label Template - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Mail Merge from DataFrame into Word Label Template (/thread-30857.html) |
Mail Merge from DataFrame into Word Label Template - PEGylated_User - Nov-10-2020 I have a DataFrame that looks like this: I have a template word document full of labels that contain mergefields. The merge field names are: Every row will be a single label so not all the labels will be the same. Each column in the same row I need on a new line in the label so each label will have 4 lines of text. I have exported the DataFrame to a csv and done the MailMerge manually to ensure that the labels will look right, but I am having trouble finding documentation on how to perform this programatically. I have tried this code so far: import pandas from mailmerge import MailMerge template_doc = "Labels.docx" data = {'FormulationNames': ['f1','f1','f1','f1','f1','f1','f1', 'f2','f2','f2','f2','f2','f2','f2'], 'FormulationLots': ['l1','l1','l1','l1','l1','l1','l1','l2','l2','l2','l2','l2','l2','l2'], 'TimePoints': ['Initial','tp1','tp1','tp1','tp1','tp1','tp1','Initial','tp2','tp2','tp2','tp2','tp2','tp2'], 'StorageConditions': ['NaN','-20C','5C','25C','30C','-20C','5C','NaN','-20C','5C','25C','30C','-20C','5C']} df = pandas.DataFrame(data) document = MailMerge(template_doc) document.merge(df) document.write(f'test.docx')When running the above code I get an error when trying to merge:
|