52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Inspect a .docx document for structure, unreplaced placeholders, and CJK font setup."""
|
|
import json
|
|
import re
|
|
import sys
|
|
import zipfile
|
|
import xml.etree.ElementTree as ET
|
|
|
|
PLACEHOLDER_RE = re.compile(r"(\{\{[^{}]+\}\}|\[TODO[^\]]*\]|TBD|待補)")
|
|
|
|
|
|
def audit(path: str) -> dict:
|
|
with zipfile.ZipFile(path) as z:
|
|
doc_xml = z.read("word/document.xml")
|
|
styles_xml = z.read("word/styles.xml") if "word/styles.xml" in z.namelist() else b""
|
|
|
|
ns = {"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}
|
|
root = ET.fromstring(doc_xml)
|
|
paragraphs = []
|
|
placeholders = []
|
|
for p in root.findall(".//w:body/w:p", ns):
|
|
txt = "".join(t.text or "" for t in p.findall(".//w:t", ns)).strip()
|
|
if txt:
|
|
paragraphs.append(txt)
|
|
for m in PLACEHOLDER_RE.findall(txt):
|
|
placeholders.append(m)
|
|
|
|
tables = root.findall(".//w:body/w:tbl", ns)
|
|
has_east_asia = (b"eastAsia" in doc_xml) or (b"eastAsia" in styles_xml)
|
|
|
|
return {
|
|
"file": path,
|
|
"non_empty_paragraphs": len(paragraphs),
|
|
"tables": len(tables),
|
|
"has_east_asia_font": has_east_asia,
|
|
"unreplaced_placeholders": placeholders[:20],
|
|
"status": "warn" if (not paragraphs or not has_east_asia or placeholders) else "ok",
|
|
}
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) < 2:
|
|
print("Usage: audit_docx.py <document.docx>", file=sys.stderr)
|
|
return 2
|
|
report = audit(sys.argv[1])
|
|
print(json.dumps(report, ensure_ascii=False, indent=2))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|