# Extract Juzhiyun manual: TOC + master-data / order / schedule chapters 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_dir = os.path.join(ROOT, "demand", "_extracted") os.makedirs(out_dir, exist_ok=True) doc = fitz.open(pdf) print(f"pages={doc.page_count} file={os.path.basename(pdf)}") toc = doc.get_toc() toc_path = os.path.join(out_dir, "juzhiyun_toc.txt") with open(toc_path, "w", encoding="utf-8") as f: for lv, title, page in toc: f.write(f"{' ' * (lv - 1)}{title}\tp{page}\n") print(f"toc entries={len(toc)} -> {toc_path}") # Keywords to pull relevant pages KEYS = [ "主数据", "物料", "BOM", "工艺路线", "工艺", "工序", "工厂", "车间", "产线", "设备", "班次", "日历", "库存", "订单", "分解", "MRP", "排产", "计划", "负荷", "甘特", "齐套", "采购", "委外", "生产订单", "工单", ] # Build page index from TOC titles matching keys interesting_pages: set[int] = set() for lv, title, page in toc: if any(k in title for k in KEYS): # include this page and next few for section body for p in range(max(1, page), min(doc.page_count, page + 4) + 1): interesting_pages.add(p) # Also scan first 30 pages of text for section headers if TOC sparse if len(interesting_pages) < 20: for i in range(min(80, doc.page_count)): text = doc[i].get_text("text") if any(k in text for k in ("主数据维护", "工艺路线", "物料管理", "订单管理", "生产计划", "排产")): interesting_pages.add(i + 1) print(f"interesting pages={len(interesting_pages)}") # Extract full text of interesting pages + nearby chunks = [] for pno in sorted(interesting_pages): text = doc[pno - 1].get_text("text") # compress blank lines text = re.sub(r"\n{3,}", "\n\n", text).strip() if text: chunks.append(f"\n\n===== PAGE {pno} =====\n{text}") body_path = os.path.join(out_dir, "juzhiyun_master_schedule.txt") with open(body_path, "w", encoding="utf-8") as f: f.write("".join(chunks)) print(f"extracted chars={sum(len(c) for c in chunks)} -> {body_path}") # Also dump ALL page texts that look like menus / module names (shorter summary) summary_lines = [] for i in range(doc.page_count): text = doc[i].get_text("text") hits = [k for k in KEYS if k in text] if hits: # first non-empty lines as context lines = [ln.strip() for ln in text.splitlines() if ln.strip()][:8] summary_lines.append(f"p{i+1} hits={hits[:6]} | {' | '.join(lines[:4])}") sum_path = os.path.join(out_dir, "juzhiyun_page_hits.txt") with open(sum_path, "w", encoding="utf-8") as f: f.write("\n".join(summary_lines)) print(f"hit pages={len(summary_lines)} -> {sum_path}")