Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Input an array of merge fields (i.e. 30 different addresses for mailing labels) #87

Open
Astrophe opened this issue Oct 15, 2020 · 5 comments

Comments

@Astrophe
Copy link

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
@dummy2501
Copy link

dummy2501 commented Dec 23, 2020

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

@Astrophe
Copy link
Author

Astrophe commented Jan 25, 2021 via email

@pmar0
Copy link

pmar0 commented Mar 24, 2022

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.

image

@Astrophe
Copy link
Author

Astrophe commented Mar 25, 2022 via email

@pmar0
Copy link

pmar0 commented Mar 25, 2022

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants