RichText.add() generates XML with incorrect element ordering - font not applied to bold/italic text
Description
The add() method in RichText generates run properties (<w:rPr>) with elements in an order that doesn't comply with the OOXML spec. Specifically, <w:rFonts> is added after <w:b> and <w:i>, but the spec requires <w:rFonts> to come before these elements. This causes Microsoft Word to ignore the font setting on bold/italic text.
Steps to reproduce
from docxtpl import DocxTemplate, RichText
doc = DocxTemplate("template.docx")
rt = RichText()
rt.add("Bold text with custom font", bold=True, font="Arial")
print(rt.xml)
Current output:
<w:r><w:rPr><w:b/><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/></w:rPr><w:t>...</w:t></w:r>
Expected output:
<w:r><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/><w:b/></w:rPr><w:t>...</w:t></w:r>
OOXML spec reference
The correct order for <w:rPr> child elements is defined in the ECMA-376 spec: rStyle, rFonts, b, bCs, i, iCs, caps, smallCaps, strike, ...
Proposed fix
In richtext.py, move the if font: block to immediately after the if style: block (around line 40), before the if bold: block.
Environment
docxtpl version: 0.20.2 Python version: 3.12
Verified on current master at 1f143fb. Pull request #626 fixed the ordering reported here.
The focused outputs are now:
font + bold: rFonts, b
font + italic: rFonts, i
style + font + bold + italic: rStyle, rFonts, b, i
I also ran tests/richtext.py and reopened its generated DOCX successfully. This issue can close as fixed on master by #626.
The current PyPI release is still 0.20.2, which predates the July 7 merge, so users of the published package will need the next release for the fix.