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

Question for docx-mailmerge #102

Open Edward-Ernest opened 2 years ago

Edward-Ernest commented 2 years ago

Recently, I am trying writing a report within which there are a lot of figures. But, there are some problems confuses me for a long time. The main problem is that the result given by ‘merge’ function will not be in the right place when I try to use a double merge domain, simple example below. In word file, I create a merge domain structure like this:

  {INCLUDEPICTURE “{MERGEFEILD path  \*MERGEFORMAT}”}  \*MERGEFORMAT}

And then I use python code to replace path with the path of the figure. But in the new word file, it becomes :

  {INCLUDEPICTURE “”}  \*MERGEFORMAT D:/test.jpg}

Could you give me an explanation why the string of the path will not be included in the “”, but outside the “” ?

iulica commented 2 years ago

Your original structure doesn't seem right, are you sure you didn't add a } too much ? Anyway, nested structures like this don't work in the docx-mailmerge. No concept of nesting is implemented so the first { is matched with the first } and so on. That's the reason. I just finished rewriting docx-mailmerge to support { IF ... } and various formats for numbers, texts and also nested structures. I will try with your example and see how it works. It's still work in progress, after adapting the tests I will add a pull request.

Edward-Ernest commented 2 years ago

Oh, yes, that's my fault. And I get your idea, thank you very much for your response. Hope your work goes well. Moreover, maybe to make the combination between the docx-mailmerge and other structure such as {INCLUDEPICTURE...} {INCLUETEXT...} in word works normally will be much helpful for word edition. Otherwise, I come from China. If my English writing confuses you, I apologize to you.

iulica commented 2 years ago

Ok, I have now tested with this { INCLUDEPICTURE "{ MERGEFIELD path }" \d } and it works fine. The only issue I get is that after opening the output docx, I have to select the whole document and press F9 to update all fields. Then the images are shown. And with your issue I have realised that I don't need to implement the IF myself, but let Word do it. So I will refactor and simplify my code.

You can try my version if you want, but be warned, it is work in progress :). I only tested it on 3.8 but it should work on any 3.5+ . You can remove the official docx-mailmerge and install mine if you want to test.

Edit: I just cleaned up the code, and added support for the updateFields setting, when necessary. auto_update_fields_on_open="auto" will set the saved docx to auto update fields when opened, whenever you have nested merge fields. This removes the need to select all document and press F9. I will add the tests when I have time and make a PR. For the moment it fixes all my needs.

Edward-Ernest commented 2 years ago

It is a good news that your work is going well. It's my pleasure to have your new version of docx-mailmerge and try to test it. But where can I find the new version of it? For the reason that the new version is not published officially, is it better to get it with my e-mail or in some ways else?

iulica commented 2 years ago

It's quite simple, it is a fork of this repository, you can click on my name and check my repositories. You can have a look at the code yourself if you want. What I did for my other work that uses docx-mailmerge:

pip uninstall docx-mailmerge
pip install docx-mailmerge2

Again, remember, WIP! If it eats your homework, I'm not to blame :).

Edward-Ernest commented 2 years ago

Thank you very much for the link. And nice joke. :)

carlos54 commented 2 years ago

Hello, finally is there any release supporting { IF ... } rule of word ?

iulica commented 2 years ago

No new release yet. I am working on it. If you need { IF ... } and want to test it, you can use my fork. Check my last answer for the install instructions. It works for me perfectly.

iulica commented 2 years ago

I have released my work in a new python package docx-mailmerge2

pip uninstall docx-mailmerge
pip install docx-mailmerge2
carlos54 commented 2 years ago

Good to know.