_bid_scan_assets.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. """列出各模块蓝图文档中的全部插图及其图题,供技术方案选图。"""
  3. import io
  4. import os
  5. import shutil
  6. import tempfile
  7. from docx import Document
  8. from docx.oxml.ns import qn
  9. ROOT = (r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目管理"
  10. r"\产互联项目管理\交付文档")
  11. HERE = os.path.dirname(os.path.abspath(__file__))
  12. OUT = os.path.join(HERE, "_bid_assets_index.txt")
  13. def scan(path, out):
  14. tmp = os.path.join(tempfile.mkdtemp(), os.path.basename(path))
  15. shutil.copy2(path, tmp)
  16. doc = Document(tmp)
  17. idx = 0
  18. last_head = ""
  19. for p in doc.paragraphs:
  20. t = p.text.strip()
  21. blips = p._p.findall(".//" + qn("a:blip"))
  22. if blips:
  23. for b in blips:
  24. idx += 1
  25. rid = b.get(qn("r:embed"))
  26. try:
  27. kb = len(doc.part.related_parts[rid].blob) // 1024
  28. except KeyError:
  29. kb = 0
  30. out.write(f" #{idx:<3} {kb:>5}KB ←节:{last_head[:34]}\n")
  31. elif t:
  32. if len(t) < 60 and (t.startswith("图") or t[:1].isdigit()
  33. or t.startswith("第")):
  34. last_head = t
  35. if t.startswith("图") and len(t) < 60:
  36. out.write(f" 图题: {t}\n")
  37. shutil.rmtree(os.path.dirname(tmp), ignore_errors=True)
  38. return idx
  39. def main():
  40. out = io.open(OUT, "w", encoding="utf-8")
  41. total = 0
  42. for dirpath, _, files in os.walk(ROOT):
  43. for fn in sorted(files):
  44. if not fn.lower().endswith(".docx") or fn.startswith("~$"):
  45. continue
  46. rel = os.path.relpath(os.path.join(dirpath, fn), ROOT)
  47. out.write(f"\n{'=' * 70}\n{rel}\n")
  48. try:
  49. n = scan(os.path.join(dirpath, fn), out)
  50. except Exception as e:
  51. out.write(f" [读取失败] {e}\n")
  52. continue
  53. out.write(f" 共 {n} 张图\n")
  54. total += n
  55. out.write(f"\n合计 {total} 张图\n")
  56. out.close()
  57. print(f"共 {total} 张图 -> {OUT}")
  58. if __name__ == "__main__":
  59. main()