elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
2.01k stars 391 forks source link

{%- weird behavior with styles #387

Open bleca opened 3 years ago

bleca commented 3 years ago

Describe the bug

  1. When I use the '{%-' merging it does not work well with style especially lists
  2. seems the '{{-' syntax from jinja is not working

To Reproduce

PYTHON minimal starting code from the doc

DOCX My house is located {%- if False -%} in urban area {%- else -%}

  1. in countryside {%- endif -%} and I love it.

Expected behavior

The list disappear and style is merged with the style of the charatacter before. I found that adding an extra space after {%- else -%} makes it work.

I had the same problem playing with styles the style of first characters is removed and it's the style of the previous character which is applied (see capture) but I didn't find any explanation about why.

Screenshots

without the extra space image image

image image

Additional context

Glad I found a workaround with the extra space but I'd like to better understand the reason.

Thanks for your work

bleca commented 3 years ago

Edit : I figured why adding a space enable the list but I still don't figure why using this syntax merge the style with the previous character. Here is a repo to reproduce : https://github.com/bleca/repro-docxtpl-issue

elapouya commented 3 years ago

The problem comes from that in your docx template, in the inner document.xml, the {% and the - are not in the same run : the regex cannot match. I have to think for a solution...

voronind commented 4 months ago

I have same. Using {%- at document beginning destroys Paper format information. We had after that len(doc.docx.sections) == 0