python-docx-template icon indicating copy to clipboard operation
python-docx-template copied to clipboard

{%- weird behavior with styles

Open bleca opened this issue 4 years ago • 3 comments

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 avatar Oct 07 '21 12:10 bleca

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

bleca avatar Oct 12 '21 11:10 bleca

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...

elapouya avatar Oct 12 '21 13:10 elapouya

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

voronind avatar Jul 05 '24 00:07 voronind