elapouya / python-docx-template

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

fixed element properties for highlighted text #482

Closed dreizehnutters closed 1 year ago

dreizehnutters commented 1 year ago

When RichText elements are used with the highlight attributes, the generated document contains errors.

Fixing the xml properties seemed to produce correct documents.

Proof-of-Concept code:

from docxtpl import RichText, DocxTemplate

tpl = DocxTemplate('poc.docx')
YELLOW = 'ffe699'

normal_string = 'Lorem ipsum dolor sit amet, consectetur adipisici elit'
rich_string = 'String with a yellow highlight'

rt_embedded = RichText(f'{rich_string}', highlight=YELLOW)

context = {
    'example' : rt_embedded,
}

tpl.render(context)
tpl.save('richtext.docx')

current:

2023-03-01 09_04_35-poc docx - Word

2023-03-01 09_05_55-Microsoft Word

2023-03-01 09_06_06-Dokument1 - Word

fixed:

2023-03-01 09_04_23-richtext docx - Word

dreizehnutters commented 1 year ago

🎉