_compare_s1_cover_struct.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """对比 S1 与本次修改文档的封面细节、图片数量与标题结构差异。"""
  3. from __future__ import annotations
  4. import zipfile
  5. from pathlib import Path
  6. from docx import Document
  7. BASE = Path(r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目管理\产互联项目管理\交付文档\Release产互联")
  8. MINE = BASE / "待确认"
  9. S1 = [
  10. "S1产销协同业务需求描述-V0.4.docx",
  11. "S1产销协同模块蓝图设计方案-V2.2.docx",
  12. "S1产销协同模块详细设计说明书-V1.2.docx",
  13. "S1产销协同模块用户操作手册-V1.2.docx",
  14. ]
  15. MINE_DOCS = [
  16. "系统集成业务需求描述-V0.5.docx",
  17. "系统集成模块蓝图设计方案-V0.5.docx",
  18. "系统集成模块详细设计说明书-V0.5.docx",
  19. "运营改善业务需求描述-V0.5.docx",
  20. "运营改善模块蓝图设计方案-V0.5.docx",
  21. "运营改善模块详细设计说明书-V0.5.docx",
  22. "九宫格智慧运营看板模块详细设计说明书-V0.1.docx",
  23. ]
  24. def img_count(path: Path) -> int:
  25. with zipfile.ZipFile(path) as z:
  26. return len([n for n in z.namelist() if n.startswith("word/media/")])
  27. def headings_of(d: Document):
  28. out = []
  29. for p in d.paragraphs:
  30. s = p.style.name
  31. if s.startswith(("Heading", "标题")) and p.text.strip():
  32. out.append(f"[{s}] {p.text.strip()}")
  33. return out
  34. def cover_info(d: Document, n=12):
  35. out = []
  36. for p in d.paragraphs[:n]:
  37. if p.text.strip():
  38. f = None
  39. for r in p.runs:
  40. if r.text.strip():
  41. f = r.font
  42. break
  43. sz = f.size.pt if f and f.size else None
  44. out.append(f" ({f.name if f else '-'}/{sz}/b={f.bold if f else '-'}) {p.text.strip()}")
  45. return out
  46. def dump(path: Path, show_heads: bool = True, head_limit: int = 100):
  47. d = Document(str(path))
  48. print(f"\n{'='*66}\n{path.name} imgs={img_count(path)} tables={len(d.tables)}")
  49. print("-- 封面段落 --")
  50. print("\n".join(cover_info(d)))
  51. if d.tables:
  52. t = d.tables[0]
  53. print(f"-- 首表 {len(t.rows)}行 --")
  54. for r in t.rows[:3]:
  55. print(" | " + " | ".join(c.text.strip().replace("\n", "/") for c in r.cells))
  56. if show_heads:
  57. hs = headings_of(d)
  58. print(f"-- 标题 {len(hs)} 个 --")
  59. print("\n".join(" " + h for h in hs[:head_limit]))
  60. def main():
  61. print("########## S1 基线 ##########")
  62. for name in S1:
  63. dump(BASE / name, show_heads=False)
  64. print("\n\n########## 本次修改的文档 ##########")
  65. for name in MINE_DOCS:
  66. dump(MINE / name, show_heads=True)
  67. if __name__ == "__main__":
  68. main()