Fix DOTX->DOCX content type for Word-compatible booking files
This commit is contained in:
@@ -113,9 +113,12 @@ def render_booking_dotx(template_path: Path, output_path: Path, data: dict) -> N
|
||||
"word/footer2.xml",
|
||||
}
|
||||
|
||||
to_docx = output_path.suffix.lower() == '.docx'
|
||||
|
||||
with zipfile.ZipFile(template_path, 'r') as zin, zipfile.ZipFile(output_path, 'w', compression=zipfile.ZIP_DEFLATED) as zout:
|
||||
for info in zin.infolist():
|
||||
raw = zin.read(info.filename)
|
||||
|
||||
if info.filename in xml_targets:
|
||||
try:
|
||||
txt = raw.decode('utf-8')
|
||||
@@ -128,6 +131,18 @@ def render_booking_dotx(template_path: Path, output_path: Path, data: dict) -> N
|
||||
raw = txt.encode('utf-8')
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if to_docx and info.filename == '[Content_Types].xml':
|
||||
try:
|
||||
txt = raw.decode('utf-8')
|
||||
txt = txt.replace(
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'
|
||||
)
|
||||
raw = txt.encode('utf-8')
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
zout.writestr(info, raw)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user