Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IF Conditionals #22

Open
dwasyl opened this issue Jun 26, 2024 · 10 comments
Open

IF Conditionals #22

dwasyl opened this issue Jun 26, 2024 · 10 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@dwasyl
Copy link

dwasyl commented Jun 26, 2024

Having had some trouble tracking down how to make IF statements work but then eventually noticed the docs:

{ IF "{ MERGEFIELD reason }" <> "" "Reason: { MERGEFIELD reason }" }

If the fields are nested inside other fields, the outer fields need to be updated in Word. This can be done by selecting everything (CTRL-a) and then update the fields (F9). There is a way to force the Word to update fields automatically when opening the document. docx-mailmerge can set this setting when saving the document. You can configure this feature by using the auto_update_fields_on_open parameter. The value always will set the setting regardless if needed or not and the value auto will only set it when necessary (when nested fields exist). The default value no will not activate this setting.

Based on that, I've been using the auto_update_fields_on_open="auto" setting which works fine, but requires opening the document and saving. In some of my use cases it would be handy to generate a complete Word document and send that the to end-user or another, but when the field needs updating it seems a bit complicated.

Would it be possible for docx-mailmerge to handle the conditional statements as part of the processing, or is that logic too complicated/unavailable?

@iulica iulica added enhancement New feature or request question Further information is requested labels Jun 26, 2024
@iulica
Copy link
Owner

iulica commented Jul 19, 2024

It's in the TODO list of another project docx-mergefields (see the documentation). At the moment only the INCLUDEPICTURE is implemented but others are on the way.

The IF is outside the scope of this project, however.

The difficulty is, that the IF field can span bigger chunks of data, like multiple paragraphs and maybe even sections. That makes it much more difficult to implement.

I may try to implement it for the simple cases first, that may be enough for 90% of the use cases and leave the rest as a followup feature.

@augustz
Copy link

augustz commented Sep 9, 2024

I've got the same need. We normally use if's around paragraphs that are optional. If client is marked as wanting a lawn blown clean with electric blowers for example, there is another paragraph that drops in. We also use it in the fee sections. If client wants XXX, then a line and price show up in the fee section.

@nappex
Copy link

nappex commented Oct 10, 2024

Could be helpful to use jinja to implement conditions?

@iulica
Copy link
Owner

iulica commented Oct 10, 2024

Could you give an example ?
I think there are other template systems to produce pdf docs that use jinja. Here we focus on using Microsoft Word compatibilty, with as few extensions as possible.

@nappex
Copy link

nappex commented Oct 10, 2024

Could you give an example ?

I think there are other template systems to produce pdf docs that use jinja. Here we focus on using Microsoft Word compatibilty, with as few extensions as possible.

Unforunately I could not I had just idea that docx is just xml and jinja condition pattern could be found there same as it found in html. I have not a lot experience if it is possible. just quick idea

@iulica
Copy link
Owner

iulica commented Oct 20, 2024

well, xml is just the underlying format and not something that you can control from inside the word document. I would say that this is not easily adapted.

On the other hand, as my microsoft office license has changed and I no longer have access to the standalone apps, I would need this feature implemented for myself as well so I will probably add it to the mergefields project in the next months. So stay alert. :)

@iulica
Copy link
Owner

iulica commented Dec 29, 2024

I just commited support for the IF fields. You can test by installing the docx-mailmerge2 directly from github.

Please test it and let me know if everything works.
To enable the IF support, you have to add the enable_experimental=True to the MailMerge object.
Also, make sure you have a space before and after the operator, as described in the docs.

@iulica
Copy link
Owner

iulica commented Dec 29, 2024

Well, I added the release 0.9.0 with the changes activated.

@ameboide
Copy link

We needed a whole paragraph to be optional, without taking any space if it wasn't there (sounds like the same scenario as #22 (comment)), and ended up implementing an "ancestor deleter" using xpath. You add { MERGEFIELD "my_field \-w:p" } somewhere in the paragraph and that will output the value if the field is not empty, or delete the whole paragraph if it is.

The implementation was very basic and ad-hoc to our needs, but it might serve as inspiration for a more polished solution: LemontechSA#1

@iulica
Copy link
Owner

iulica commented Dec 31, 2024

@ameboide Thanks for the comment. Your solution is no longer needed, when the IF field works as intended, right ? Or am I missing something?

Although mailmerge has some custom features (e.g. merge rows) that do not work in the Microsoft Word, we try to keep this at a minimum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants