aps-agent/scripts/_extract_juzhiyun_chapters.py

33 lines
1.3 KiB
Python

# Extract key chapters from Juzhiyun manual for master data + plan chain
from __future__ import annotations
import glob, os, re, fitz
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
pdf = glob.glob(os.path.join(ROOT, "demand", "*.pdf"))[0]
out = os.path.join(ROOT, "demand", "_extracted", "juzhiyun_key_chapters.txt")
doc = fitz.open(pdf)
# Manual page numbers (printed) → PDF page index from hits:
# printed p40 = PDF p46 for 主数据管理
# We'll extract by PDF page ranges from page_hits
RANGES = [
("4.4 主数据管理总述+生产模型", 46, 62),
("4.4.2 设备模型", 63, 75),
("4.4.5 工艺模型(物料/BOM/工序/工艺路线)", 105, 130),
("4.8 工艺设计", 155, 163),
("4.9 计划管理(订单/采购/外协/排产)", 163, 184),
("4.11/4.13 备料与库存", 199, 226),
]
chunks = []
for title, start, end in RANGES:
chunks.append(f"\n\n########## {title} PDF {start}-{end} ##########\n")
for pno in range(start, min(end, doc.page_count) + 1):
text = doc[pno - 1].get_text("text")
text = re.sub(r"\n{3,}", "\n\n", text).strip()
chunks.append(f"\n----- PDF p{pno} -----\n{text}\n")
with open(out, "w", encoding="utf-8") as f:
f.write("".join(chunks))
print(f"wrote {out} chars={sum(len(c) for c in chunks)}")