extract_plan_sources.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. # -*- coding: utf-8 -*-
  2. from pathlib import Path
  3. from docx import Document
  4. ROOT = Path(__file__).resolve().parent
  5. FILES = [
  6. "在线考试系统技术文案(方案B).docx",
  7. "在线考试系统两方案可行性评审报告.docx",
  8. ]
  9. for name in FILES:
  10. doc = Document(ROOT / name)
  11. out = []
  12. out.append(f"SOURCE: {name}")
  13. for block in doc.element.body.iterchildren():
  14. if block.tag.endswith("}p"):
  15. for p in doc.paragraphs:
  16. if p._p is block:
  17. if p.text.strip():
  18. out.append(f"[{p.style.name}] {p.text}")
  19. break
  20. elif block.tag.endswith("}tbl"):
  21. for table in doc.tables:
  22. if table._tbl is block:
  23. out.append("[TABLE]")
  24. for row in table.rows:
  25. out.append(" | ".join(cell.text.replace("\n", " / ") for cell in row.cells))
  26. out.append("[/TABLE]")
  27. break
  28. target = ROOT / (Path(name).stem + "_提取.txt")
  29. target.write_text("\n".join(out), encoding="utf-8")
  30. print(f"{target.name}: {len(out)} blocks")