Bouke / docx-mailmerge

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.
MIT License
274 stars 104 forks source link

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

Open Astrophe opened 3 years ago

Astrophe commented 3 years ago

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

dummy2501 commented 3 years ago

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 commented 3 years ago

Thank you so much for getting back to me! For anyone that is interested, I ended up using a different library called docxjs available here: https://docx.js.org/#/ that worked really well for me!

Ryan Walter

Innovator Astrophe Solutions (202) 642-5141 astrophesolutions.com

http://astrophesolutions.com/

On Wed, Dec 23, 2020 at 2:34 PM dummy2501 notifications@github.com wrote:

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.get_merge_fields() document.merge( field1 = df.iloc[x][0], field2 = df.iloc[x][2], field3 = df.iloc[x][2], fieldn = df.iloc[x][n] )

document.write('output.docx')

`

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Bouke/docx-mailmerge/issues/87#issuecomment-750442881, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJHY2P3KN6FCXH7J3CJLKSDSWJA4JANCNFSM4SSJIOPQ .

-- The information transmitted is intended only for the person or entity to which it is addressed and may contain proprietary, business-confidential, and/or privileged material. If you are not the intended recipient of this message you are hereby notified that any use, review, retransmission, dissemination, distribution, reproduction or any action taken in reliance upon this message is prohibited. If you received this in error, please contact the sender and delete the material from any computer. If you have entered into a NDA with Astrophe Enterprises. LLC, the information transmitted is included in the definition of "Confidential Information".

This email is for discussion purposes only and cannot be used to create a binding contract.

pmar0 commented 2 years ago

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 commented 2 years ago

Pete,

You're right, pandas didn't work. However docx worked brilliantly! It's a bit code heavy cause I basically had to setup each line of 3 labels and then loop on that up to 11 on a page, and then loop on that for multi page, so it's not the cleanest, BUT it does work just fine :) Maybe I'll publish it as a fork on the docx library one day.

On Thu, Mar 24, 2022 at 5:17 PM Pete @.***> wrote:

I just wanted to note here that the solution provided by @dummy2501 https://github.com/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 https://github.com/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: image] https://user-images.githubusercontent.com/43326223/159972689-0dc08eab-6cff-44ad-b03a-b008498cadb0.png

— Reply to this email directly, view it on GitHub https://github.com/Bouke/docx-mailmerge/issues/87#issuecomment-1077855589, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJHY2P4GNIKA7DUTN5DGEADVBSPSXANCNFSM4SSJIOPQ . You are receiving this because you were mentioned.Message ID: @.***>

-- The information transmitted is intended only for the person or entity to which it is addressed and may contain proprietary, business-confidential, and/or privileged material. If you are not the intended recipient of this message you are hereby notified that any use, review, retransmission, dissemination, distribution, reproduction or any action taken in reliance upon this message is prohibited. If you received this in error, please contact the sender and delete the material from any computer. If you have entered into a NDA with Astrophe Enterprises, LLC or any of its subsidiaries or joint ventures, including OneGift LLC, Astrophe Solutions LLC, or Pelham Enterprises, LLC and subsidiaries, the information transmitted is included in the definition of "Confidential Information".

This email is for discussion purposes only and cannot be used to create a binding contract.

pmar0 commented 2 years ago

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.