Input an array of merge fields (i.e. 30 different addresses for mailing labels)
Astrophe opened this issue · 5 comments
Hi, is there a way to input an array of names and addresses so that my .docx export file has all different labels with the same merge fields but different values? I apologize if this is an obvious question, but I've been working on this problem for a while, without a solution.
Current Behavior
Currently, when I use the document.merge() method, I can create a page of identical labels, but I can't iterate through an array of values, so that all my labels are for a different mailing address.
Possible Solution
Input an array or even an excel spreadsheet where every row is a different label.
Your Environment
- Python version: Python 3.8.1
- docx-mailmerge version: Version: 0.5.0
You can do this easily with pandas and a for loop!
import pandas as pd
from mailmerge import MailMerge
df = pd.read_csv('spreadsheet.csv')
template = 'document.docx'
for x in range(len(df)):
document = MailMerge(template)
document.merge(
field1 = df.iloc[x][0],
field2 = df.iloc[x][1],
field3 = df.iloc[x][2],
fieldn = df.iloc[x][n-1]
)
document.write(outputVar + '.docx')
#Will overwrite the file each time if you don't assign a dynamic variable
I just wanted to note here that the solution provided by @dummy2501 doesn't solve the problem being asked. From what I can tell of that solution, it creates tons of different files each saved with one of the addresses. I'm guessing it's a miscommunication, so I'll clarify to what I think @Astrophe was trying to say:
See the image below. I have 12 of these labels on a singular page of a docx that I use for mail merging regularly. I'm only showing one row for example purposes. When using mail merge normally, it would take the matching information from one row (so one product description, price, UPC, etc) and populate a single one of the four labels there. Each label would be a different product, price etc (aka a different row in the excel doc). This is the typical function of mail merge for labels in Word, so hopefully that and my explanation are enough to get the point across.
From what I can tell, this library doesn't have any functionality that can perform that task, so I'm going to look into this docx library for JS and hope that can help.
Yeah, Id be curious to see the setup you have, myself.
I tried every which method down to modifying my mail merge doc in an attempt to better fit this libraries functionality; but it's just not made to do a page full of different labels, like you said.