dump_docx.py 815 B

1234567891011121314151617
  1. # -*- coding: utf-8 -*-
  2. import zipfile, re, json, sys, io
  3. sys.stdout.reconfigure(encoding="utf-8")
  4. p = r"C:\Users\aroun\Desktop\在线考试系统\在线考试系统技术文案(方案B).docx"
  5. z = zipfile.ZipFile(p)
  6. xml = z.read("word/document.xml").decode("utf-8")
  7. paras = re.findall(r"<w:p[ >].*?</w:p>", xml, re.S)
  8. out = []
  9. for pa in paras:
  10. texts = re.findall(r"<w:t[^>]*>(.*?)</w:t>", pa, re.S)
  11. t = "".join(texts)
  12. t = t.replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", '"')
  13. m = re.search(r'<w:pStyle w:val="([^"]+)"', pa)
  14. out.append({"style": m.group(1) if m else "", "text": t})
  15. with open(r"C:\Users\aroun\Desktop\在线考试系统\_paras.json", "w", encoding="utf-8") as f:
  16. json.dump(out, f, ensure_ascii=False, indent=0)
  17. print("paras", len(out))