_tmp_scan_blueprints.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. """扫描交付文档目录下各模块蓝图设计方案:列出标题层级与内嵌图片清单。"""
  3. import os
  4. import shutil
  5. import sys
  6. import tempfile
  7. sys.stdout.reconfigure(encoding="utf-8", errors="replace")
  8. from docx import Document
  9. from docx.oxml.ns import qn
  10. ROOT = r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目管理\产互联项目管理\交付文档"
  11. OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  12. "_tmp_blueprint_scan.txt")
  13. TARGETS = []
  14. for dirpath, _, files in os.walk(ROOT):
  15. for fn in files:
  16. if fn.endswith(".docx") and "蓝图设计方案" in fn and "Tmp" not in dirpath \
  17. and "更新" not in dirpath and "(1)" not in fn:
  18. TARGETS.append(os.path.join(dirpath, fn))
  19. TARGETS.sort()
  20. tmpdir = tempfile.mkdtemp()
  21. lines = []
  22. for path in TARGETS:
  23. rel = os.path.relpath(path, ROOT)
  24. local = os.path.join(tmpdir, os.path.basename(path))
  25. try:
  26. shutil.copy2(path, local)
  27. doc = Document(local)
  28. except Exception as e:
  29. lines.append(f"\n### {rel} [打不开: {type(e).__name__}]")
  30. continue
  31. imgs = [p for p in doc.part.package.parts
  32. if p.partname.startswith("/word/media/")]
  33. total_kb = sum(len(p.blob) for p in imgs) / 1024
  34. lines.append("\n" + "=" * 78)
  35. lines.append(f"### {rel}")
  36. lines.append(f" 段落 {len(doc.paragraphs)} 表格 {len(doc.tables)} "
  37. f"图片 {len(imgs)} 张({total_kb:,.0f} KB)")
  38. # 标题结构
  39. lines.append(" --- 标题 ---")
  40. for p in doc.paragraphs:
  41. sname = p.style.name if p.style is not None else ""
  42. t = p.text.strip()
  43. if not t:
  44. continue
  45. lvl = None
  46. if sname.startswith("Heading"):
  47. try:
  48. lvl = int(sname.split()[-1])
  49. except ValueError:
  50. lvl = 1
  51. elif sname.startswith("标题"):
  52. lvl = 1
  53. if lvl and lvl <= 3:
  54. lines.append(" " + " " * (lvl - 1) + f"[H{lvl}] {t[:70]}")
  55. # 图片:找出图片所在段落的上下文(图题)
  56. lines.append(" --- 图片上下文 ---")
  57. n = 0
  58. for i, p in enumerate(doc.paragraphs):
  59. if p._p.findall(".//" + qn("a:blip")):
  60. n += 1
  61. cap = ""
  62. for j in range(i + 1, min(i + 4, len(doc.paragraphs))):
  63. c = doc.paragraphs[j].text.strip()
  64. if c:
  65. cap = c[:70]
  66. break
  67. prev = ""
  68. for j in range(i - 1, max(i - 4, -1), -1):
  69. c = doc.paragraphs[j].text.strip()
  70. if c:
  71. prev = c[:70]
  72. break
  73. lines.append(f" img{n:>2} 上文: {prev}")
  74. lines.append(f" 下文: {cap}")
  75. with open(OUT, "w", encoding="utf-8") as f:
  76. f.write("\n".join(lines))
  77. shutil.rmtree(tmpdir, ignore_errors=True)
  78. print(f"扫描 {len(TARGETS)} 份蓝图 -> {OUT}")
  79. for t in TARGETS:
  80. print(" ", os.path.relpath(t, ROOT))