Python Forum
Mail Merge from DataFrame into Word Label Template
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mail Merge from DataFrame into Word Label Template
#1
I have a DataFrame that looks like this:
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  –°ombine (Merge) word documents using python-docx Lancellot 1 2,035 May-12-2021, 11:07 AM
Last Post: toothedsword
  Mail issue Mihil 3 381 Dec-03-2020, 05:25 AM
Last Post: Mihil
  I try to import data from Excel table to Word Template. NewbiePyPy 0 574 Oct-21-2020, 12:25 PM
Last Post: NewbiePyPy
  Dataframe to template Kristenl2784 0 469 Jul-01-2020, 04:24 PM
Last Post: Kristenl2784
  Python Speech recognition, word by word AceScottie 6 10,341 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 1,533 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  Mail Faiyaz 1 1,059 Sep-24-2018, 02:29 PM
Last Post: Larz60+
  Pands DataFrame merge question FORTITUDE 0 931 Sep-20-2018, 01:14 PM
Last Post: FORTITUDE
  difference between word: and word[:] in for loop zowhair 2 1,830 Mar-03-2018, 07:24 AM
Last Post: zowhair
  Simple e-mail sender, that automatically changes subject for every e-mail joker 2 1,715 Aug-24-2017, 09:45 AM
Last Post: joker

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020