# -*- coding: utf-8 -*- """列出各模块蓝图文档中的全部插图及其图题,供技术方案选图。""" import io import os import shutil import tempfile from docx import Document from docx.oxml.ns import qn ROOT = (r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目管理" r"\产互联项目管理\交付文档") HERE = os.path.dirname(os.path.abspath(__file__)) OUT = os.path.join(HERE, "_bid_assets_index.txt") def scan(path, out): tmp = os.path.join(tempfile.mkdtemp(), os.path.basename(path)) shutil.copy2(path, tmp) doc = Document(tmp) idx = 0 last_head = "" for p in doc.paragraphs: t = p.text.strip() blips = p._p.findall(".//" + qn("a:blip")) if blips: for b in blips: idx += 1 rid = b.get(qn("r:embed")) try: kb = len(doc.part.related_parts[rid].blob) // 1024 except KeyError: kb = 0 out.write(f" #{idx:<3} {kb:>5}KB ←节:{last_head[:34]}\n") elif t: if len(t) < 60 and (t.startswith("图") or t[:1].isdigit() or t.startswith("第")): last_head = t if t.startswith("图") and len(t) < 60: out.write(f" 图题: {t}\n") shutil.rmtree(os.path.dirname(tmp), ignore_errors=True) return idx def main(): out = io.open(OUT, "w", encoding="utf-8") total = 0 for dirpath, _, files in os.walk(ROOT): for fn in sorted(files): if not fn.lower().endswith(".docx") or fn.startswith("~$"): continue rel = os.path.relpath(os.path.join(dirpath, fn), ROOT) out.write(f"\n{'=' * 70}\n{rel}\n") try: n = scan(os.path.join(dirpath, fn), out) except Exception as e: out.write(f" [读取失败] {e}\n") continue out.write(f" 共 {n} 张图\n") total += n out.write(f"\n合计 {total} 张图\n") out.close() print(f"共 {total} 张图 -> {OUT}") if __name__ == "__main__": main()