elapouya/python-docx-template

Formatted text after subdoc tag corrupts generated docx (cannot be opened by Microsoft Word)

Opened this issue · 0 comments

Describe the bug

When creating a word with a subdoc tag, if there is some formatted text after the subdoc tag, the generated docx document cannot be opened using Microsoft Word. This can happen simply because some underline is added around the subdoc tag (a simple space underlined seem to be enough to corrupt the resulting document.
It might be caused by a run (the underlined text after the subdoc) being "forcefully" included in the subdoc generated paragraph.

To Reproduce

Attached is the subdoc test found in the library code, slightly modified to add some formatting text around the subdoc tag in the template. The template is the only part that we need to change to see this behaviour.
subdoc_corrupted.zip

Expected behavior

The generated document should not be corrupted and Microsoft Word should be able to open it.

Screenshots

Capture d’écran 2024-10-15 154636 Capture d’écran 2024-10-15 155334 Capture d’écran 2024-10-15 155512

Additional context

We cannot always use the {{p subdoc}} syntax because we need some label on the same line. This works fine if there is no underlined text after the tag, but it is easy to add it without being aware and break everything. Since we need to use Microsoft Word to open the files, it is hard to see if the generated docx is corrupted only by using unit tests.