Files
bid-doctor/spikes/s1-convert/fix_charset.py
YANG JIANKUAN 604e889ca4 docs(architecture): 完成技术验证 S1–S4,确定 FR-M2-05/06 目标值
- spikes/:Word 转 PDF(Gotenberg)、格式识别、原句坐标、DeepSeek 抽取四项验证代码与两版提示词;样本派生产物和 .env 不进 git
- tech-validation.md:完整结论。四项都可行:WPS 字体字符集要修正并改用行高调整过的字体;格式识别不需要 Docling;表格内切句待改进;抽取需第三道依据校验与规则兜底
- architecture.md:§3.5 加预处理与字体包,§3.6 证据校验改为三道,§10 回填结论
- 03 功能需求与 decisions.md(TV1):召回 ≥ 99%、采纳准确率 ≥ 95%、页码 100%、高亮正文 ≥ 98% / 表格 ≥ 95%

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 12:46:49 +08:00

32 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""修正 WPS 写错的字体字符集:中文字体(名字含汉字,或是常见中文字体英文名)被标成 SYMBOL(02) 时改成 GB2312(86)。
输入、输出都是 DOCX。用法:python -I fix_charset.py in.docx out.docx"""
import re, sys, zipfile
CJK_LATIN = {"SimSun", "NSimSun", "SimHei", "FangSong", "KaiTi", "Microsoft YaHei", "DengXian"}
def is_cjk_font(name: str) -> bool:
return bool(re.search(r"[一-鿿]", name)) or name in CJK_LATIN
def fix_font_table(xml: str) -> tuple[str, list[str]]:
fixed = []
def repl(m):
name, body = m.group(1), m.group(2)
if is_cjk_font(name) and re.search(r'w:charset w:val="02"', body):
fixed.append(name)
body = body.replace('w:charset w:val="02"', 'w:charset w:val="86"')
return f'<w:font w:name="{name}">{body}</w:font>'
return re.sub(r'<w:font w:name="([^"]+)">(.*?)</w:font>', repl, xml, flags=re.S), fixed
def main(src, dst):
with zipfile.ZipFile(src) as zin, zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED) as zout:
for item in zin.infolist():
data = zin.read(item.filename)
if item.filename == "word/fontTable.xml":
xml, fixed = fix_font_table(data.decode("utf-8"))
data = xml.encode("utf-8")
print("fixed:", fixed)
zout.writestr(item, data)
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])