# -*- coding: utf-8 -*- from pathlib import Path from docx import Document ROOT = Path(__file__).resolve().parent FILES = [ "在线考试系统技术文案(方案B).docx", "在线考试系统两方案可行性评审报告.docx", ] for name in FILES: doc = Document(ROOT / name) out = [] out.append(f"SOURCE: {name}") for block in doc.element.body.iterchildren(): if block.tag.endswith("}p"): for p in doc.paragraphs: if p._p is block: if p.text.strip(): out.append(f"[{p.style.name}] {p.text}") break elif block.tag.endswith("}tbl"): for table in doc.tables: if table._tbl is block: out.append("[TABLE]") for row in table.rows: out.append(" | ".join(cell.text.replace("\n", " / ") for cell in row.cells)) out.append("[/TABLE]") break target = ROOT / (Path(name).stem + "_提取.txt") target.write_text("\n".join(out), encoding="utf-8") print(f"{target.name}: {len(out)} blocks")