docx skill: validate.py crashes/fails on Windows due to cp1252 encoding (fontTable.xml decode + console print)
Summary
scripts/office/validate.py in the docx skill fails on Windows with a UnicodeDecodeError while reading word/fontTable.xml, and separately crashes with an unhandled UnicodeEncodeError in its own status output. Both are encoding bugs in the validator itself, reproducible on a completely empty python-docx document — not related to any specific document content.
Repro
from docx import Document
Document().save("empty-test.docx")python scripts/office/validate.py empty-test.docxObserved output
FAILED - Found NEW validation errors:
word\fontTable.xml: 1 new error(s)
- 'charmap' codec can't decode byte 0x9d in position 1295: character maps to <undefined>
word\settings.xml: 1 new error(s)
- Element '{...}zoom': The attribute '{...}percent' is required but missing.
Traceback (most recent call last):
...
File ".../validators/docx.py", line 249, in compare_paragraph_counts
print(f"\nParagraphs: {original_count} → {new_count} ({diff_str})")
UnicodeEncodeError: 'charmap' codec can't encode character '→' in position 16: character maps to <undefined>Root cause (as far as we could tell)
fontTable.xmldecode error — the defaultpython-docxtemplate embeds a font table that includes East Asian font names (e.g. "MS Mincho"). Whatever file-reading path the validator uses forfontTable.xmlopens it with the platform default encoding (cp1252on Windows) instead of explicitutf-8, so bytes outside the cp1252 codepage raiseUnicodeDecodeError.- Console print crash —
validators/docx.py'scompare_paragraph_countsprints a literal→(→) viaprint(), which on a Windows console with the defaultcp1252code page raisesUnicodeEncodeErrorand crashes the whole validation run with a traceback instead of a clean pass/fail result.
Workaround we're using: run with PYTHONIOENCODING=utf-8 (and PYTHONUTF8=1) set, which avoids both issues. But this isn't documented anywhere in the skill, and a first-time Windows user hits a hard crash with the default python.exe.
Suggested fix
- Open all XML files read for validation with
encoding="utf-8"explicitly (orerrors="replace"as a fallback), rather than relying on the platform locale. - Replace the
print(f"...→...")with either an ASCII-safe arrow (->) or force UTF-8 stdout (sys.stdout.reconfigure(encoding="utf-8")at the top of the script, Python 3.7+) so the validator doesn't crash on Windows consoles with a non-UTF-8 code page.
Separate, unrelated finding worth noting
The zoom element missing w:percent in settings.xml is a real (if minor) gap in python-docx's default template — not a validator bug, just flagging it here since it showed up in the same repro. We worked around it with a small post-processing step on our end.
Environment
- Windows 11
- Python 3.14 (also likely affects any Python on Windows with default
cp1252locale) python-docx(latest via pip)- Skill:
document-skills:docx(this repo,skills/docx)
Source: anthropics/skills