import zipfile, sys from xml.etree import ElementTree as ET path = "职来后端接口联调说明.docx" z = zipfile.ZipFile(path) xml = z.read("word/document.xml").decode("utf-8") ns = {"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"} root = ET.fromstring(xml) def para_text(p): texts = [] for t in p.iter("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t"): texts.append(t.text or "") return "".join(texts) body = root.find("w:body", ns) out = [] for el in body: tag = el.tag.split("}")[-1] if tag == "p": out.append(para_text(el)) elif tag == "tbl": rows = [] for tr in el.iter("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tr"): cells = [para_text(p) for p in tr.iter("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p")] rows.append(" | ".join(cells)) out.append("\n[TABLE]\n" + "\n".join(rows) + "\n") full = "\n".join(out) with open("scripts/docx_text.txt", "w", encoding="utf-8") as f: f.write(full) print("TOTAL CHARS:", len(full)) for i, line in enumerate(out): if any(k in line for k in ["OpenAPI", "接口", "路径", "endpoint", "/api", "GET", "POST", "§7", "七、"]): print(f"[{i}] {line[:140]}")