Question for docx-mailmerge
Edward-Ernest opened this issue · 10 comments
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 “” ?
- Python version: 3.10
- docx-mailmerge version: 0.50
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.
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.
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.
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?
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 :).
Thank you very much for the link. And nice joke. :)
Hello, finally is there any release supporting { IF ... } rule of word ?
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.
I have released my work in a new python package docx-mailmerge2
pip uninstall docx-mailmerge
pip install docx-mailmerge2
Good to know.