1

I would like to seek your help on my below issue. I have been struggling with this issue for a few days.

I have a word template as shown below in docx.

===============

enter image description here

Name: {{ Personal_name }}
Age:{{ Personal_Age }}
Teenager/Adult: {% if Personal_Age ==18 %} 
Teenager 
{% else %} 
Adult 
{% endif %}

It is expected that the "Teenager/Adult" value should be "Teenager" as per the conditional statement. However, the value is still showing "Adult", which indicates the conditional statement does not work. Would everyone let me know what is the issue on it?

Also, does everyone let me know how to turn the "Age" background colour to be red with a comment box pop up if there is null input in "Age"?

Here is my scripts for your reference as well.

========================================================

from docxtpl import DocxTemplate,RichText
doc=DocxTemplate('test_word_2.docx')
context={'Personal_name':'Charlie','Personal_Age':RichText(18,color='FF0000',bold=True)}
doc.render(context)
doc.save('test_word_2_test'+'.docx')  

===================================================

The current final output is attached here

enter image description here

Thanks everyone.

Tony Tang
  • 47
  • 6
  • I have not used that library, but you are passing a `RichText(18, ...)` instance which gets checked against the integer `18`. – ivvija Jan 26 '23 at 10:10
  • I cannot pass 18 into RichText? But i need to use the color function and the bold style inside the RichText function. – Tony Tang Jan 26 '23 at 10:12
  • No you can, but the compare seems like it will never evaluate to `True`. RichText is not the value 18 anymore, but a wrapper which also holds formatting info. It is something completly different. The easiest way would adding a new variable to `context` like `Personal_Age_int` which holds just `18` and can be compared in the template – ivvija Jan 26 '23 at 10:19

1 Answers1

3

An equals check between RichText and int will always be false since they are different types and no __eq__ override exists for checking against the text content. RichText source code

Plus it seems like there is no easy way to get the RichText content.

Easiest solution is adding a new context variable for programmatic handling while keeping the RichText for display:

Edit: I did not change the logic, but it seems like you want to check < 18 since from age 18 you are an adult?

from docxtpl import DocxTemplate,RichText

doc = DocxTemplate('test_word_2.docx')

personal_age = 18

context = {
  'Personal_name': 'Charlie',
  'Personal_Age': RichText(personal_age, color='FF0000', bold=True),
  'Personal_Age_int': personal_age
}
doc.render(context)
doc.save('test_word_2_test'+'.docx')  
Name: {{ Personal_name }}
Age:{{ Personal_Age }}
Teenager/Adult: {% if Personal_Age_int == 18 %} 
Teenager 
{% else %} 
Adult 
{% endif %}
ivvija
  • 482
  • 4
  • 9
  • Thanks ivvija. It works. But, do you have any idea on my second question? I tired to use a cellbg to do so, but it ends up an error message pop up. "Encountered unknown tag 'tr'." – Tony Tang Jan 26 '23 at 10:55
  • my second question is here:Also, does everyone let me know how to turn the "Age" background colour to be red with a comment box pop up if there is null input in "Age"? – Tony Tang Jan 26 '23 at 10:56