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
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 1,814 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
Question How can I merge several images via loop using the information of a dataframe? noahverner1995 1 1,396 Dec-31-2021, 05:03 AM
Last Post: noahverner1995
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Сombine (Merge) word documents using python-docx Lancellot 1 11,360 May-12-2021, 11:07 AM
Last Post: toothedsword
  Mail issue Mihil 3 2,588 Dec-03-2020, 05:25 AM
Last Post: Mihil
  I try to import data from Excel table to Word Template. NewbiePyPy 0 3,217 Oct-21-2020, 12:25 PM
Last Post: NewbiePyPy
  Dataframe to template Kristenl2784 0 1,415 Jul-01-2020, 04:24 PM
Last Post: Kristenl2784
  Python Speech recognition, word by word AceScottie 6 15,858 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 4,718 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  smtplib mail without subject anna 2 2,431 Apr-24-2019, 05:44 AM
Last Post: anna

Forum Jump:

User Panel Messages

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