Fix booking sheet Cyrillic rendering by RTF-escaping inserted values
This commit is contained in:
@@ -31,6 +31,20 @@ def _extract_amount(s: str) -> float:
|
||||
return 0.0
|
||||
|
||||
|
||||
def rtf_escape(value: str) -> str:
|
||||
out = []
|
||||
for ch in value:
|
||||
code = ord(ch)
|
||||
if ch in ['\\', '{', '}']:
|
||||
out.append('\\' + ch)
|
||||
elif code > 127:
|
||||
signed = code if code < 32768 else code - 65536
|
||||
out.append(f"\\u{signed}?")
|
||||
else:
|
||||
out.append(ch)
|
||||
return ''.join(out)
|
||||
|
||||
|
||||
def render_booking_rtf(template_path: Path, output_path: Path, data: dict) -> None:
|
||||
if not template_path.exists():
|
||||
return
|
||||
@@ -46,8 +60,7 @@ def render_booking_rtf(template_path: Path, output_path: Path, data: dict) -> No
|
||||
if txt is None:
|
||||
txt = raw.decode("utf-8", errors="ignore")
|
||||
|
||||
# Явно фиксируем UTF-8 кодировку RTF (для совместимости)
|
||||
txt = txt.replace("\\rtf1\\ansi", "\\rtf1\\ansi\\ansicpg65001", 1)
|
||||
# Сохраняем исходный заголовок шаблона RTF (кодировку задаёт сам шаблон)
|
||||
|
||||
total_num = _extract_amount(data.get("total", ""))
|
||||
prepay_num = _extract_amount(data.get("prepay", ""))
|
||||
@@ -71,7 +84,7 @@ def render_booking_rtf(template_path: Path, output_path: Path, data: dict) -> No
|
||||
}
|
||||
|
||||
for k, v in repl.items():
|
||||
txt = txt.replace(k, str(v))
|
||||
txt = txt.replace(k, rtf_escape(str(v)))
|
||||
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
output_path.write_text(txt, encoding="utf-8")
|
||||
|
||||
Reference in New Issue
Block a user