iulica / docx-mailmerge

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

Merging docx with mergefields in multiple sections #26

Closed Mtokoniewski closed 2 months ago

Mtokoniewski commented 2 months ago

Hey! I have a file that has merge fields separated from each other by continuous section break. I am looking for the option to select entire document as a template, rather than a single section.

Expected Behavior

I hoped that on printing merge fields, all mergefields within the document would become printed, meaning that I could merge them without changing formatting.

Current Behavior

Currently, only mergefields in the earliest document section are being read.

Possible Solution

Would it be possible to make the docx-mailmerge read entire document as template, even if the document originally contains multiple sections?

iulica commented 2 months ago

can you provide a sample docx and the python code you are using to replicate the problem ?

iulica commented 2 months ago

Here is a test I made, and it works perfectly, as supposed.

def test_multisection(self):
        document, root_elem = self.merge_templates(
            "test_multisection.docx",
            [{"sec1": "v1", "sec2": "v2"}, {"sec1": "v3", "sec2": "v4"}],
            separator="continuous_section",
            output="tests/test_output_multisection.docx",
        )
image

test_output_multisection.docx test_multisection.docx

Mtokoniewski commented 2 months ago

Mail Merging.zip Here is my attempt, fitted with Desired Output.

iulica commented 2 months ago

Hi, if you have sections, you should use exclusively section seaparators. So replace the page_break with nextPage_section and it will work.

iulica commented 2 months ago

Let me know if it works, and in this case, please close the issue.

Mtokoniewski commented 2 months ago

Hey! Issue Solved, it worked.