python-docx-template
python-docx-template copied to clipboard
error occurs when trying to escape the blank new line in front /behind of a table if -%} syntax is used
Describe the bug
error occurs when trying to escape the blank new line in front /behind of a table if -%} syntax is used #445
To Reproduce
dynamic_table_tpl.docx plain_table_output.docx the docs template like this (please find the source file attached): scripts code:
from docxtpl import DocxTemplate
tpl = DocxTemplate('templates/plain_table.docx')
content_table = {
"type":'table',
"data": {
"columns":['date', 'type', 'before', 'after'],
"values":[['2021-12-31', 'register', 'name: Liu; amount: ****;...; percent: *%;', 'name: Liu; amount: ***; ...; percent: *%;'], ['2020-11-20', 'register', 'name: Liu; amount: ****;... percent: **%;', 'name: Liu; amount: ****;...; percent: *%;'], ['2018-06-27', 'register', 'name: Zhou; amount: ***; ...; percent: **%', 'name: Ding; amount: ***; ...; percent: **%'], ['2019-03-22', 'register', 'name: Liu; amount: ***; ...; percent: **%', 'name: Liu; amount: ****;...; percent: **%']]
}
}
content_text = {
"type":'raw_text',
"data": "############this is a text block##########"
}
content_form = {
"type":'form',
"data": [{'key': 'title', 'value': 'abnormal', 'span': 2}, {'key': 'dispatch', 'value': 'xiehui', 'span': 1}, {'key': 'SN', 'value': None, 'span': 1}, {'key': 'result', 'value': 'abnormal', 'span': 1}, {'key': 'details', 'value': None, 'span': 1}, {'key': 'result', 'value': '', 'span': 2}]
}
tpl.render({"content_table":content_table,"content_text":content_text,"content_form":content_form})
tpl.save('output/plain_table_output.docx')
Expected behavior
according to jinjia2 syntax, {%- and -%} should be added if I want to merge the lines. But error occurs when {%- and -%} are used before and after a table.
Screenshots
- syntax error when red highlighted - is added
- table will not appear when the yellow highlighted - is added
- the desired output
Additional context
I noticed that the issue is related to #445 {%p may be usefull for {% if something %}, but not for {% macro func(x) %}. it seems that
{%p macro merge(flag) %}
{%p set test = True %}
{%p if flag %}
Flag is true
{%p else %}
Flag is False
{%p endif %}
{%p endmacro %}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{{ merge(True) }}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{{ merge(False) }}
the macro func will never be parsed which generates a docx like below:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You should not use {%- ... -%}
and {{- .. -}}
here, Please use {%p ... %}
and {{ ... }}
instead
You should not use
{%- ... -%}
and{{- .. -}}
here, Please use{%p ... %}
and{{ ... }}
instead
Yes, thanks for your reply.
but {%p ... %}
only works for {%p if ... %}
but not for {%p macro func(...) %}
.
since we can not use {%p macro func(...) %}
and {%p endmacro %}
, there are still two blank lines generated by {%p macro merge(flag) %}
and {%p endmacro %}
please find the examples above this comment. https://github.com/elapouya/python-docx-template/issues/488#issuecomment-1512526137
I never tried macro during development. the goal of {%p ... %}
syntax is to replace the xml code for the paragraph where it is located by {% ... %}
alone. It should work, I will try to debug...