_tmp_read_bp_text.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. """导出无图蓝图(S9/运营诊断/运营改善/系统集成/ChatBI)的正文与表格,供撰写技术方案取材。"""
  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. from docx.table import Table
  11. from docx.text.paragraph import Paragraph
  12. ROOT = r"C:\Users\skygu\OneDrive\Projects\AIDOP\项目\项目管理\产互联项目管理\交付文档"
  13. OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  14. "_tmp_bp_text.txt")
  15. FILES = [
  16. "S9/S9运营指标与智慧看板模块蓝图设计方案.docx",
  17. "运营诊断/运营诊断模块蓝图设计方案.docx",
  18. "运营改善/运营改善模块蓝图设计方案.docx",
  19. "系统集成/系统集成模块蓝图设计方案.docx",
  20. "ChatBI/ChatBI智能报表模块蓝图设计方案.docx",
  21. ]
  22. tmpdir = tempfile.mkdtemp()
  23. out = []
  24. for rel in FILES:
  25. src = os.path.join(ROOT, rel.replace("/", os.sep))
  26. local = os.path.join(tmpdir, os.path.basename(src))
  27. shutil.copy2(src, local)
  28. doc = Document(local)
  29. out.append("\n" + "#" * 80)
  30. out.append(f"### {rel}")
  31. out.append("#" * 80)
  32. for el in doc.element.body.iterchildren():
  33. if el.tag == qn("w:p"):
  34. p = Paragraph(el, doc)
  35. t = p.text.strip()
  36. if not t:
  37. continue
  38. s = p.style.name if p.style is not None else ""
  39. pref = f"[{s}] " if s.startswith(("Heading", "标题")) else ""
  40. out.append(pref + t)
  41. elif el.tag == qn("w:tbl"):
  42. tb = Table(el, doc)
  43. out.append(f"--- TABLE {len(tb.rows)}x{len(tb.columns)} ---")
  44. for r in tb.rows:
  45. seen, cells = set(), []
  46. for c in r.cells:
  47. if c._tc in seen:
  48. continue
  49. seen.add(c._tc)
  50. cells.append(c.text.strip().replace("\n", " / "))
  51. out.append(" | " + " | ".join(cells))
  52. shutil.rmtree(tmpdir, ignore_errors=True)
  53. with open(OUT, "w", encoding="utf-8") as f:
  54. f.write("\n".join(out))
  55. print(f"导出 {len(FILES)} 份 -> {OUT} ({sum(len(x) for x in out):,} 字符)")