Nov-10-2020, 01:01 PM
I have a DataFrame that looks like this:
I have tried this code so far:
Output: FormulationNames FormulationLots TimePoints StorageConditions
0 f1 l1 Initial NaN
1 f1 l1 tp1 -20C
2 f1 l1 tp1 5C
3 f1 l1 tp1 25C
4 f1 l1 tp1 30C
5 f1 l1 tp2 -20C
6 f1 l1 tp2 5C
7 f2 l2 Initial NaN
8 f2 l2 tp1 -20C
9 f2 l2 tp1 5C
10 f2 l2 tp1 25C
11 f2 l2 tp1 30C
12 f2 l2 tp2 -20C
13 f2 l2 tp2 5C
I have a template word document full of labels that contain mergefields. The merge field names are:Output: print(document.get_merge_fields())
{'TimePoints', 'FormulationNames', 'StorageConditions', 'FormulationLots'}
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:
Error:Exception has occurred: ValueError
The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
File "C:\Users\02260235\Downloads\Test App\ST.py", line 14, in <module>
document.merge(df)