| 12345678910111213141516171819202122232425262728293031323334353637 |
- # -*- coding: utf-8 -*-
- """把生成的 docx 按正文顺序导出成纯文本,用于人工校对(只读,不改文档)。"""
- import sys
- import docx
- from docx.table import Table
- from docx.text.paragraph import Paragraph
- def main(path, out_path):
- d = docx.Document(path)
- lines = []
- for child in d.element.body.iterchildren():
- tag = child.tag.split("}")[-1]
- if tag == "p":
- par = Paragraph(child, d)
- if par.text.strip():
- lines.append("[%s] %s" % (par.style.name, par.text))
- elif tag == "tbl":
- t = Table(child, d)
- lines.append("<表格 %d 行 x %d 列>" % (len(t.rows), len(t.columns)))
- for r in t.rows:
- lines.append(" | " + " | ".join(c.text.replace("\n", " ") for c in r.cells))
- text = "\n".join(lines)
- with open(out_path, "w", encoding="utf-8") as fh:
- fh.write(text)
- bad = [ln for ln in lines if "**" in ln]
- bt = [ln for ln in lines if "`" in ln]
- print("lines=%d chars=%d bold_markup=%d backtick=%d" % (len(lines), len(text), len(bad), len(bt)))
- for ln in bad[:5] + bt[:5]:
- print(" !", ln[:120])
- return 0
- if __name__ == "__main__":
- raise SystemExit(main(sys.argv[1], sys.argv[2]))
|