iulica / docx-mailmerge

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

Dynamic Bullet/Numbered List #19

Closed keith-gray-powereng closed 5 months ago

keith-gray-powereng commented 5 months ago

This may be a more general Mail Merge question, but I want to have a bullet/numbered list in my template and then pass a list of items to be included in that list as part of the mail merge. Do you know if that is possible with Word Mail Merge? If so, is it possible with this library? If so, could you point me to the relevant code please?

Thank you.

iulica commented 5 months ago

Not sure if this is possible with Word, and I am pretty sure it is not possible with docx-mailmerge. But just to be sure, you would want a bullet list to be just like a table row. Have you tried to make a table of one row and one column, hide the margins, and inside the one cell add the bullet/numbered list with the mailmerge code. Then, you can use the merge_table feature or just give the list of values for the mailmerge keyword and use the merge_templates. I don't know if it will work, but it's worth a try.

If you provide a sample docx and python code I can give it a try myself.

keith-gray-powereng commented 5 months ago

Thank you for the quick response. I did try the table implementation and it did work as expected. I was hoping to avoid using tables for formatting because that can be confusing to the user if they want to edit the resulting merged document. However, I decided to change the format of my document and make use of more columns in that table. You can consider this resolved.

As a side note, I also attempted merge_templates but it copied the entire page for each item in the list.

iulica commented 5 months ago

You most probably used the merge_templates wrong, like this: document.merge_templates([ {'field1': "Foo"}, {'field1': "Bar"}, ], separator='page_break')

Each dictionary is a copy of the entire document. What you want is that 'field1' is a list instead of just one value, like this: document.merge_templates([ {'field1': ["Doc1 Foo", "Doc1 Bar"], 'field_outside_list': 'value document1'}, {'field1': ["Doc2 Foo", "Doc2 Bar", "Doc2 John"], 'field_outside_list': 'value document2'}, ], separator='page_break')

This will create a 2 page document, each page containing the table with the rows for the field1.

If you only need one document, use merge instead of merge_templates or just use one dict in the array.