# -*- coding: utf-8 -*- """扫描交付文档目录下各模块蓝图设计方案:列出标题层级与内嵌图片清单。""" import os import shutil import sys import tempfile sys.stdout.reconfigure(encoding="utf-8", errors="replace") from docx import Document from docx.oxml.ns import qn ROOT = r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目管理\产互联项目管理\交付文档" OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "_tmp_blueprint_scan.txt") TARGETS = [] for dirpath, _, files in os.walk(ROOT): for fn in files: if fn.endswith(".docx") and "蓝图设计方案" in fn and "Tmp" not in dirpath \ and "更新" not in dirpath and "(1)" not in fn: TARGETS.append(os.path.join(dirpath, fn)) TARGETS.sort() tmpdir = tempfile.mkdtemp() lines = [] for path in TARGETS: rel = os.path.relpath(path, ROOT) local = os.path.join(tmpdir, os.path.basename(path)) try: shutil.copy2(path, local) doc = Document(local) except Exception as e: lines.append(f"\n### {rel} [打不开: {type(e).__name__}]") continue imgs = [p for p in doc.part.package.parts if p.partname.startswith("/word/media/")] total_kb = sum(len(p.blob) for p in imgs) / 1024 lines.append("\n" + "=" * 78) lines.append(f"### {rel}") lines.append(f" 段落 {len(doc.paragraphs)} 表格 {len(doc.tables)} " f"图片 {len(imgs)} 张({total_kb:,.0f} KB)") # 标题结构 lines.append(" --- 标题 ---") for p in doc.paragraphs: sname = p.style.name if p.style is not None else "" t = p.text.strip() if not t: continue lvl = None if sname.startswith("Heading"): try: lvl = int(sname.split()[-1]) except ValueError: lvl = 1 elif sname.startswith("标题"): lvl = 1 if lvl and lvl <= 3: lines.append(" " + " " * (lvl - 1) + f"[H{lvl}] {t[:70]}") # 图片:找出图片所在段落的上下文(图题) lines.append(" --- 图片上下文 ---") n = 0 for i, p in enumerate(doc.paragraphs): if p._p.findall(".//" + qn("a:blip")): n += 1 cap = "" for j in range(i + 1, min(i + 4, len(doc.paragraphs))): c = doc.paragraphs[j].text.strip() if c: cap = c[:70] break prev = "" for j in range(i - 1, max(i - 4, -1), -1): c = doc.paragraphs[j].text.strip() if c: prev = c[:70] break lines.append(f" img{n:>2} 上文: {prev}") lines.append(f" 下文: {cap}") with open(OUT, "w", encoding="utf-8") as f: f.write("\n".join(lines)) shutil.rmtree(tmpdir, ignore_errors=True) print(f"扫描 {len(TARGETS)} 份蓝图 -> {OUT}") for t in TARGETS: print(" ", os.path.relpath(t, ROOT))