| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723 |
- #!/usr/bin/env python3
- """艾德范思(Advanced Bios)肠道微生态检测报告 PDF 采集解析器。
- 将 PDF 解析为与既有 per-report JSON(如 501999942-某人.json)一致的结构化格式,
- 供后续同厂商真实报告批量复用。
- 依赖:PyMuPDF (fitz)。用法:
- python parse_aidefansi_report.py <report.pdf> [-o output.json]
- 输出结构(JSON):
- 文件名 / 格式(aidefansi) / 总页数 / 报告机构 / 个人信息 / 报告概述 /
- 各整体健康评估分表(名称-数值-状态三元组)/
- 有益菌检出 / 条件致病菌检出 / 致病菌检出 /
- 分项检测参考 / 肠道菌群组成(门/属/特征菌) /
- 指标详解(含相关介绍/食物来源/注意事项) /
- 过敏相关菌 / 疾病相关菌 / 疾病介绍与调理建议 / 报告声明
- 解析策略:按词坐标重建表格。表格区域 = [表头行 y, 下一个 ※/【/注:/检测者 标记 y),
- 行锚点 = 最右列状态词,行带 = 相邻锚点中点;无表头的续页按"当前章节"归属,
- 不依赖纯文本顺序,可跨页累积同一章节。
- """
- import json
- import re
- import sys
- from pathlib import Path
- try:
- import fitz
- except ImportError:
- sys.exit("需要 PyMuPDF: pip install pymupdf")
- # ---------------------------------------------------------------- 基础工具
- def page_words(page):
- """返回按 (y, x) 排序的词列表 [(x, y, text), ...]。"""
- words = [(w[0], w[1], w[4]) for w in page.get_text("words")]
- words.sort(key=lambda w: (round(w[1], 1), w[0]))
- return words
- def cluster_rows(words, tol=9.0):
- """按 y 坐标聚类成行,返回 [[(x,y,text),...], ...](行内按 x 排序)。"""
- rows = []
- for w in words:
- if rows and abs(w[1] - rows[-1][0][1]) <= tol:
- rows[-1].append(w)
- else:
- rows.append([w])
- for r in rows:
- r.sort(key=lambda w: w[0])
- return rows
- END_MARKERS = ("※", "【", "注:", "检测者:", "审核者:", "第")
- def next_marker_y(words, after_y, markers=END_MARKERS):
- """after_y 之后第一个表格结束标记的 y 坐标;找不到返回 None。"""
- cands = [y for x, y, t in words if y > after_y and any(t.startswith(m) for m in markers)]
- return min(cands) if cands else None
- def clean(s):
- return re.sub(r"\s+", "", s or "")
- def find_headings(words):
- """识别左端起编号的章节标题,返回 [(y, 编号, 名称), ...]。"""
- heads = []
- for row in cluster_rows(words, tol=4.0):
- if not row:
- continue
- first = row[0]
- if not (55 <= first[0] <= 100):
- continue
- t0 = first[2]
- # 支持多级编号(如 1.7.1 条件致病菌)
- m = re.match(r"^(\d+(?:\.\d+)*)[\.、\s]?(.*)$", t0)
- if not m:
- continue
- code, rest = m.group(1), m.group(2)
- name = rest + "".join(t for x, y, t in row[1:] if x < 320)
- heads.append((first[1], code, clean(name)))
- return heads
- # ---------------------------------------------------------------- 封面/机构
- def parse_cover(doc):
- text = doc[1].get_text()
- info = {}
- for key, pat in {
- "姓名": r"姓名[::]\s*\n?([^\n]+)",
- "性别": r"姓别[::]\s*\n?([^\n]+)",
- "出生年月": r"出生年月[::]\s*\n?([^\n]+)",
- "样本条码": r"样本条码[::]\s*\n?([^\n]+)",
- "采样日期": r"采样日期[::]\s*\n?([^\n]+)",
- "报告日期": r"报告日期[::]\s*\n?([^\n]+)",
- }.items():
- m = re.search(pat, text)
- if m:
- v = m.group(1).strip()
- info[key] = "" if v in ("年-月-日",) else v
- return info
- def parse_org(doc):
- text = doc[-1].get_text()
- org = {}
- for pat, key in [
- (r"公司官网[::]\s*(\S+)", "官网"),
- (r"联系电话[::]\s*(\S+)", "电话"),
- (r"联系邮箱[::]\s*(\S+)", "邮箱"),
- (r"公司地址[::]\s*(.+)$", "地址"),
- (r"^(.*?实验室)\s*$", "名称"),
- ]:
- m = re.search(pat, text, re.M)
- if m:
- org[key] = m.group(1).strip()
- return org
- # ---------------------------------------------------------------- 表格解析器
- def parse_triplet_table(words, y_start, y_end, name_x_max=300.0, val_range=(300.0, 432.0)):
- """名称|评估值|检测结果 三列表。锚点=检测结果列(x>=val_range[1])。"""
- hi = y_end if y_end is not None else 10 ** 9
- scope = [w for w in words if y_start < w[1] < hi]
- anchors = sorted([w for w in scope if w[0] >= val_range[1]], key=lambda w: w[1])
- if not anchors:
- return []
- bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
- out = []
- for i, anc in enumerate(anchors):
- cells = [w for w in scope if bounds[i] <= w[1] < bounds[i + 1]]
- cells.sort(key=lambda w: (w[1], w[0]))
- name = clean("".join(t for x, y, t in cells if x < name_x_max))
- value = clean("".join(t for x, y, t in cells if val_range[0] <= x < val_range[1]))
- if name or value:
- out.append({"名称": name, "数值": value, "状态": clean(anc[2])})
- return out
- def parse_abx_table(words, y_start, y_end):
- """类别|名称|评估值|检测结果 四列表(抗生素耐药性,类别列纵向合并,按行只取名称)。"""
- hi = y_end if y_end is not None else 10 ** 9
- scope = [w for w in words if y_start < w[1] < hi]
- anchors = sorted([w for w in scope if w[0] >= 432], key=lambda w: w[1])
- if not anchors:
- return []
- bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
- out = []
- for i, anc in enumerate(anchors):
- cells = sorted([w for w in scope if bounds[i] <= w[1] < bounds[i + 1]], key=lambda w: (w[1], w[0]))
- name = clean("".join(t for x, y, t in cells if 230 <= x < 335))
- value = clean("".join(t for x, y, t in cells if 335 <= x < 432))
- cat = ""
- m = re.match(r"^(.{0,8}?(?:内酰胺类|氨基糖苷类|多肽类|四环素类))(?!$)(.+)$", name)
- if m: # 类别列与名称列粘连时剥离
- raw, name = m.group(1), m.group(2)
- cat = next((c for c in CATEGORY_PREFIXES if c[1:] in raw or c in raw), raw)
- if name or value:
- out.append({"类别": cat, "名称": name, "数值": value, "状态": clean(anc[2])})
- return out
- def parse_quad_table(words, y_start, y_end, cols):
- """单行四列表(门水平/属水平/特征菌/指标详情/参考表)。cols=[(x_min,x_max),...]。"""
- hi = y_end if y_end is not None else 10 ** 9
- scope = [w for w in words if y_start < w[1] < hi]
- out = []
- for row in cluster_rows(scope, tol=9.0):
- cells = [clean("".join(t for x, y, t in row if lo <= x < hi2)) for lo, hi2 in cols]
- if any(cells):
- out.append(cells)
- return out
- LATIN_RE = re.compile(r"^[A-Za-z][A-Za-z0-9 _().,'\-]*$")
- # 结果评价列合法状态词(用于过滤"结果评价"表头折行产生的假锚点)
- STATUS_WORDS = {"正常", "偏低", "偏高", "未检出", "注意", "耐药", "缺乏", "超标", "检出"}
- CN_STOP = ",。;、!?…"
- SPECIES_RE = re.compile(r"(?<=[\u4e00-\u9fff])([a-z][A-Za-z0-9\-]*)")
- def pick_cn(text):
- """取中文名的合理片段:首个中文标点之前。"""
- text = clean(text or "")
- for ch in CN_STOP:
- if ch in text:
- text = text.split(ch)[0]
- break
- return text
- def parse_bacteria_table(words, y_start, y_end):
- """菌名|功能说明|检测结果(%)|结果评价 表(有益菌/条件致病菌/致病菌)。
- 锚点=结果评价列(x>=470)且y在表头之下、文本为合法状态词;
- 左列按语种拆分:纯拉丁词归拉丁名,其余归菌名/功能说明。
- """
- hi = y_end if y_end is not None else 10 ** 9
- scope = [w for w in words if y_start < w[1] < hi]
- anchors = sorted([w for w in scope if w[0] >= 470 and w[1] > y_start + 6 and w[2] in STATUS_WORDS],
- key=lambda w: w[1])
- if not anchors:
- return []
- bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
- out = []
- for i, anc in enumerate(anchors):
- cells = [w for w in scope if bounds[i] <= w[1] < bounds[i + 1]]
- left = sorted([w for w in cells if w[0] < 160], key=lambda w: w[1])
- cn, latin, extra_desc = "", [], []
- if left:
- first_y = left[0][1]
- cn = clean("".join(t for x, y, t in left if abs(y - first_y) < 6))
- for x, y, t in left:
- if abs(y - first_y) < 6:
- continue
- if LATIN_RE.match(t):
- latin.append(t)
- else:
- extra_desc.append(t)
- mid = sorted([w for w in cells if 160 <= w[0] < 390], key=lambda w: (w[1], w[0]))
- desc = "".join(t for x, y, t in mid)
- val = sorted([w for w in cells if 390 <= w[0] < 470], key=lambda w: (w[1], w[0]))
- # 中文名取首个标点前的片段;过长说明整段叙述溢入左列
- cn = pick_cn(cn)
- if cn and len(cn) > 24:
- extra_desc.insert(0, cn)
- cn = ""
- if not latin and desc:
- m = re.search(r"([A-Z][a-z]+ [a-z]+)", desc)
- if m:
- latin = [m.group(1)]
- # 描述列中被挤出的种名(如 "伤寒enterica")补回拉丁名
- if latin and desc:
- for sp in SPECIES_RE.findall(desc):
- if len(latin) < 3 and len(sp) >= 4 and not sp.isupper():
- latin.append(sp)
- out.append({
- "中文名": cn,
- "拉丁名": " ".join(latin).strip(),
- "功能说明": ("".join(extra_desc) + desc).strip(),
- "检测结果%": clean("".join(t for x, y, t in val)),
- "结果评价": clean(anc[2]),
- })
- return out
- DIR_RE = re.compile(r"^(正相关|负相关|补充)([\d.]*)$")
- DIRECTION_WORDS = {"正相关", "负相关", "补充", "相关", "性"}
- def parse_correlation_table(words, y_start, y_end):
- """菌种|相关性|检测结果|参考范围|结果评价 表(过敏/疾病相关菌)。"""
- hi = y_end if y_end is not None else 10 ** 9
- scope = [w for w in words if y_start < w[1] < hi]
- anchors = sorted([w for w in scope if w[0] >= 440 and w[1] > y_start + 6
- and w[2] not in ("结果评价", "评价", "价")], key=lambda w: w[1])
- if not anchors:
- return []
- bounds = [y_start] + [(anchors[i][1] + anchors[i + 1][1]) / 2 for i in range(len(anchors) - 1)] + [hi]
- out = []
- for i, anc in enumerate(anchors):
- cells = [w for w in scope if bounds[i] <= w[1] < bounds[i + 1]]
- left = sorted([w for w in cells if w[0] < 190], key=lambda w: w[1])
- cn, latin = [], []
- direction, value, rng = "", "", ""
- if left:
- first_y = left[0][1]
- for x, y, t in left:
- if t in DIRECTION_WORDS:
- direction = direction or t
- elif LATIN_RE.match(t):
- latin.append(t)
- elif abs(y - first_y) < 6:
- cn.append(t)
- cn, latin = clean("".join(cn)), " ".join(latin).strip()
- right = sorted([w for w in cells if 190 <= w[0] < 440], key=lambda w: w[0])
- nums = []
- for x, y, t in right:
- m = DIR_RE.match(t)
- if m:
- direction = m.group(1)
- if m.group(2):
- nums.append(m.group(2))
- elif re.match(r"^[\d.]+$", t):
- nums.append(t)
- if nums:
- value = nums[0]
- rest = nums[1:]
- if rest:
- rng = rest[0] if len(rest) == 1 else f"{rest[0]}-{rest[-1]}"
- out.append({
- "菌种": cn,
- "拉丁名": latin.strip(),
- "相关性": direction,
- "检测结果": value,
- "参考范围": rng,
- "结果评价": clean(anc[2]),
- })
- return out
- # ---------------------------------------------------------------- 文本块
- BLOCK_LABELS = ["相关介绍", "食物来源", "注意事项", "温馨提示", "结果提示", "疾病介绍", "调理建议", "检测结果", "检测数据"]
- def extract_text_blocks(words, y_start, y_end):
- """提取 相关介绍/食物来源/注意事项/疾病介绍/调理建议 等文本块(保留原文换行)。"""
- hi = y_end if y_end is not None else 10 ** 9
- scope = sorted([w for w in words if y_start < w[1] < hi], key=lambda w: (round(w[1], 1), w[0]))
- blocks, cur_label, lines, cur_y = {}, None, [], None
- for x, y, t in scope:
- line_start = x < 100
- stem = t.rstrip("::")
- if line_start and stem in BLOCK_LABELS:
- if cur_label and lines:
- blocks[cur_label] = "\n".join(lines)
- cur_label, lines, cur_y = stem, [], y
- continue
- if line_start and (t.startswith("※") or t.startswith("【") or re.match(r"^\d+(\.\d+)?[\.、\s]?$", t)):
- if cur_label and lines:
- blocks[cur_label] = "\n".join(lines)
- cur_label, lines, cur_y = None, [], None
- continue
- if cur_label is not None:
- if cur_y is not None and abs(y - cur_y) > 6:
- lines.append("\n")
- cur_y = y
- lines.append(t)
- if cur_label and lines:
- blocks[cur_label] = "".join(lines)
- return {k: v for k, v in blocks.items() if v}
- # ---------------------------------------------------------------- 章节定义
- SUMMARY_SECTIONS = [ # (标题关键词, 输出键, 解析器)
- ("肠道菌群整体健康状况", "报告概述", "triplet"),
- ("肠道免疫力评估", "肠道免疫力评估", "triplet"),
- ("营养物质代谢能力", "营养物质代谢能力", "triplet"),
- ("短链脂肪酸合成能力", "短链脂肪酸合成能力", "triplet"),
- ("维生素评估", "维生素评估", "triplet"),
- ("天然色素合成能力", "天然色素合成能力", "triplet"),
- ("氨基酸合成能力", "氨基酸合成能力", "triplet"),
- ("谷胱甘肽合成能力", "谷胱甘肽合成能力", "triplet"),
- ("胆汁酸合成能力", "胆汁酸合成能力", "triplet"),
- ("健康相关矿物质评估", "健康相关矿物质评估", "triplet"),
- ("多轴系统风险评估", "多轴系统风险评估", "triplet"),
- ("毒素降解能力评估", "毒素降解能力评估", "triplet"),
- ("抗生素耐药性评估", "抗生素耐药性评估", "abx"),
- ("肠道菌群过敏风险评估", "肠道菌群过敏风险评估", "triplet"),
- ("肠道菌群与疾病相关风险评估", "肠道菌群与疾病相关风险评估", "triplet"),
- ]
- CATEGORY_PREFIXES = ("β-内酰胺类", "氨基糖苷类", "多肽类", "四环素类") # 用于粘连剥离与归一化
- ABX_CATEGORY = {
- "青霉素": "β-内酰胺类", "头孢菌素": "β-内酰胺类",
- "链霉素": "氨基糖苷类", "卡那霉素": "氨基糖苷类", "庆大霉素": "氨基糖苷类", "新霉素": "氨基糖苷类",
- "万古霉素": "多肽类", "四环素": "四环素类",
- }
- DETAIL_TITLES = {
- "1.1": "肠道菌群状态", "1.2": "肠道微生物定植抗力", "1.3": "菌群多样性", "1.4": "肠型",
- "1.5": "厚壁菌门/拟杆菌门(F/B)", "1.8": "肠道抗炎能力", "1.9": "肠道免疫力",
- "2.1": "糖(碳水化合物)", "2.2": "蛋白质", "2.3": "脂肪", "2.4": "嘌呤",
- "3.1": "甲酸", "3.2": "乙酸", "3.3": "丙酸", "3.4": "丁酸", "3.5": "戊酸",
- "5.1": "类胡萝卜素", "5.2": "类黄酮", "5.3": "花青素",
- "7.1": "苯甲酸盐", "7.2": "苯氨基甲酸酯", "7.3": "氯代烷烃", "7.4": "氯代环己烷", "7.5": "氯苯",
- "7.6": "甲苯", "7.7": "二甲苯", "7.8": "硝基甲苯", "7.9": "苯乙烯", "7.10": "乙苯",
- "7.11": "阿特拉津", "7.12": "己内酰胺", "7.13": "双酚A", "7.14": "二噁英", "7.15": "萘",
- "7.16": "多环芳烃",
- "9.1": "胃胀", "9.2": "腹胀和排气", "9.3": "便秘", "9.4": "腹泻", "9.5": "炎症性肠病(IBD)",
- "9.6": "肠易激综合征(IBS)", "9.7": "克罗恩病", "9.8": "结直肠癌", "9.9": "肥胖",
- "9.10": "II型糖尿病", "9.11": "高脂血", "9.12": "痛风(高尿酸)", "9.13": "非酒精性脂肪肝",
- "9.14": "精神分裂", "9.15": "抑郁、焦虑", "9.16": "阿尔兹海默病", "9.17": "帕金森", "9.18": "痤疮",
- }
- for i, vt in enumerate(["A", "B1", "B2", "B3", "B5", "B6", "B7", "B9", "B12", "C", "K", "D", "E"]):
- DETAIL_TITLES[f"4.{i + 1}"] = f"维生素{vt}"
- AA_TITLES = ["赖氨酸", "丝氨酸", "亮氨酸", "色氨酸", "苯丙氨酸", "缬氨酸", "组氨酸", "半胱氨酸",
- "酪氨酸", "丙氨酸", "脯氨酸", "苏氨酸", "谷氨酸", "异亮氨酸", "精氨酸", "甲硫氨酸",
- "甘氨酸", "天冬氨酸"]
- for i, aa in enumerate(AA_TITLES):
- DETAIL_TITLES[f"6.{i + 1}"] = aa
- ALLERGY_TITLES = {"8.1": "花生过敏", "8.2": "鸡蛋过敏", "8.3": "坚果过敏", "8.4": "牛奶过敏",
- "8.5": "甲壳类和软体动物过敏", "8.6": "麸质敏感"}
- # ---------------------------------------------------------------- 主流程
- def parse(pdf_path):
- doc = fitz.open(str(pdf_path))
- pages = [page_words(p) for p in doc]
- result = {
- "文件名": Path(pdf_path).stem,
- "格式": "aidefansi",
- "总页数": len(doc),
- "报告机构": parse_org(doc),
- "个人信息": parse_cover(doc),
- }
- # ---- 1) 整体健康分表(PDF 第 5-19 页)----
- sections = {key: [] for _, key, _ in SUMMARY_SECTIONS}
- cur_key = None
- for pno in range(4, 19):
- page, words = doc[pno], pages[pno]
- # 章节起点:标题词命中(左边缘 x<100 或页面顶部 y<200)
- starts = []
- for title, key, _kind in SUMMARY_SECTIONS:
- for r in page.search_for(title):
- if r.x0 < 100 or r.y0 < 200:
- starts.append((r.y0, key))
- starts.sort()
- # 表头行("名称")
- header_ys = sorted([y for x, y, t in words if t == "名称"])
- # 表格区域:[表头 y, 下一个结束标记 y)
- regions = []
- for hy in header_ys:
- y_end = next_marker_y(words, hy)
- # 区域归属:本页中表头上方最近的章节起点,否则继承上一页章节
- owner = None
- for sy, key in starts:
- if sy < hy:
- owner = key
- regions.append((hy, y_end, owner or cur_key))
- if owner:
- cur_key = owner
- # 无表头且无章节起点的续页:顶部区域归属当前章节
- if not header_ys and not starts:
- y_end = next_marker_y(words, 0)
- if y_end and y_end > 100:
- regions.append((100, y_end, cur_key))
- for hy, y_end, key in regions:
- if key is None:
- continue
- kind = next(kd for t, k, kd in SUMMARY_SECTIONS if k == key)
- parser = parse_abx_table if kind == "abx" else parse_triplet_table
- rows = parser(words, hy, y_end)
- sections[key].extend(rows)
- if starts:
- cur_key = starts[-1][1]
- overview_text = doc[4].get_text()
- m = re.search(r"您的肠道总评分为[::]\s*\n?\s*(\d+)分", overview_text)
- total_score = int(m.group(1)) if m else None
- result["报告概述"] = {r["名称"]: {"数值": r["数值"], "状态": r["状态"]}
- for r in sections.pop("报告概述", [])}
- if total_score is not None:
- result["报告概述"]["肠道总评分"] = {"数值": str(total_score), "状态": ""}
- for key in ["肠道免疫力评估", "营养物质代谢能力", "短链脂肪酸合成能力", "维生素评估",
- "天然色素合成能力", "氨基酸合成能力", "谷胱甘肽合成能力", "胆汁酸合成能力",
- "健康相关矿物质评估", "多轴系统风险评估", "毒素降解能力评估",
- "肠道菌群过敏风险评估", "肠道菌群与疾病相关风险评估"]:
- result[key] = sections.get(key, [])
- result["抗生素耐药性评估"] = [
- {"类别": ABX_CATEGORY.get(r["名称"]) or r.get("类别", ""),
- "名称": r["名称"], "数值": r["数值"], "状态": r["状态"]}
- for r in sections.get("抗生素耐药性评估", [])
- ]
- # ---- 2) 细菌检出表(PDF 第 26-34 页:1.6 有益菌 / 1.7.1 条件致病菌 / 1.7.2 致病菌)----
- bact = {"有益菌检出": [], "条件致病菌检出": [], "致病菌检出": []}
- bact_notes = {}
- cur_bact = None
- for pno in range(25, 34):
- words = pages[pno]
- heads = find_headings(words)
- bact_heads = []
- for hy, code, name in heads:
- if code == "1.6":
- bact_heads.append((hy, "有益菌检出"))
- elif code == "1.7.1":
- bact_heads.append((hy, "条件致病菌检出"))
- elif code == "1.7.2":
- bact_heads.append((hy, "致病菌检出"))
- elif code == "1.7" and name.startswith("条件致病菌"):
- bact_heads.append((hy, "条件致病菌检出"))
- elif code == "1.7" and name.startswith("致病菌"):
- bact_heads.append((hy, "致病菌检出"))
- header_ys = sorted([y for x, y, t in words if t == "菌名"])
- if bact_heads:
- cur_bact = bact_heads[-1][1]
- if cur_bact is None:
- continue
- if header_ys:
- y_start = header_ys[0]
- y_end = next_marker_y(words, y_start)
- bact[cur_bact].extend(parse_bacteria_table(words, y_start, y_end))
- else:
- # 续页:顶部到第一个结束标记
- y_end = next_marker_y(words, 0)
- if y_end and y_end > 100:
- bact[cur_bact].extend(parse_bacteria_table(words, 100, y_end))
- # 结果提示/注释块
- blocks = extract_text_blocks(words, 0, None)
- for lbl in ("结果提示", "温馨提示"):
- if blocks.get(lbl):
- bact_notes.setdefault(cur_bact, {}).setdefault(lbl, blocks[lbl])
- result.update(bact)
- if bact_notes:
- result["细菌检出附注"] = bact_notes
- # ---- 3) 分项说明页(PDF 第 21-114 页)----
- details, ref_tables = [], {}
- correlations, disease_notes = {}, {}
- cur_sec = None # (code, name, kind)
- for pno in range(20, 114):
- words = pages[pno]
- heads = find_headings(words)
- sec_list = []
- for hy, code, name in heads:
- kind = "allergy" if code in ALLERGY_TITLES else ("disease" if code.startswith("9.") else "detail")
- sec_list.append((hy, code, name, kind))
- # 页首无归属文本块 -> 继承上一章节
- first_head_y = sec_list[0][0] if sec_list else None
- if cur_sec and first_head_y:
- blocks = extract_text_blocks(words, 0, first_head_y)
- _absorb_blocks(cur_sec, blocks, correlations, disease_notes, details, ref_tables)
- if not sec_list:
- # 整页续上一章节
- if cur_sec:
- _parse_section_page(doc, pno, words, cur_sec, 0, None,
- correlations, disease_notes, details, ref_tables)
- continue
- for idx, (hy, code, name, kind) in enumerate(sec_list):
- y_end = sec_list[idx + 1][0] if idx + 1 < len(sec_list) else None
- cur_sec = (code, name, kind)
- _parse_section_page(doc, pno, words, cur_sec, hy, y_end,
- correlations, disease_notes, details, ref_tables)
- result["指标详解"] = details
- if ref_tables:
- result["分项检测参考"] = ref_tables
- if correlations:
- result["过敏相关菌"] = {k: v for k, v in correlations.items() if k in ALLERGY_TITLES.values()}
- result["疾病相关菌"] = {k: v for k, v in correlations.items() if k not in ALLERGY_TITLES.values()}
- if disease_notes:
- result["疾病介绍与调理建议"] = disease_notes
- # ---- 4) 菌群组成(PDF 第 117-121 页)----
- quad_cols = [(90, 240), (240, 320), (320, 410), (410, 520)]
- comp = {"门水平": [], "属水平": [], "特征菌科属种": []}
- prev_comp = None
- for pno in range(116, 121):
- words = pages[pno]
- text = doc[pno].get_text()
- y_end = next_marker_y(words, 0, markers=("第",)) # 过滤页脚"第 N 页"
- rows = []
- if "您的肠道菌群组成(门水平)" in text:
- prev_comp, rows = "门水平", parse_quad_table(words, 190, y_end, cols=quad_cols)
- elif "您的肠道菌群组成(属水平)" in text:
- prev_comp, rows = "属水平", parse_quad_table(words, 180, y_end, cols=quad_cols)
- elif "特征菌科属种" in text:
- prev_comp, rows = "特征菌科属种", parse_quad_table(words, 180, y_end, cols=quad_cols)
- elif prev_comp: # 续页
- rows = parse_quad_table(words, 100, y_end, cols=quad_cols)
- if rows and prev_comp:
- rows = [r for r in rows if not (len(r) >= 2 and r[1] in ("页",) or (r[0].startswith("参考文献")))]
- comp[prev_comp].extend(rows)
- result["肠道菌群组成"] = {
- "门水平": [{"拉丁名": r[0], "中文名": r[1], "检测结果%": r[2], "参考范围%": r[3]}
- for r in comp["门水平"] if len(r) >= 4],
- "属水平": [{"拉丁名": r[0], "中文名": r[1], "检测结果%": r[2], "参考范围%": r[3]}
- for r in comp["属水平"] if len(r) >= 4],
- "特征菌科属种": [{"拉丁学名": r[0], "中文名称": r[1], "检测结果%": r[2], "阈值%": r[3]}
- for r in comp["特征菌科属种"] if len(r) >= 4],
- }
- # ---- 4.1) 组成表:先拆分粘连的"拉丁+中文",再建词典回填中文名 ----
- for r in result["肠道菌群组成"]["特征菌科属种"]:
- m = re.match(r"^([A-Za-z][A-Za-z0-9 _.()\-']*)([\u4e00-\u9fff].*)$", r["拉丁学名"])
- if m:
- r["拉丁学名"], r["中文名称"] = m.group(1).strip(), (m.group(2) + r["中文名称"]).strip()
- latin_cn = {}
- for key in ("有益菌检出", "条件致病菌检出", "致病菌检出"):
- for r in result.get(key, []):
- la, cn = r.get("拉丁名", ""), r.get("中文名", "")
- cn0 = re.split(r"[((]", cn)[0]
- # 同拉丁名取最短中文名,避免整句叙述进词典
- if la and cn0 and len(cn0) <= 24:
- if la not in latin_cn or len(cn0) < len(latin_cn[la]):
- latin_cn[la] = cn0
- # 疾病/过敏相关菌表也纳入词典(含大量干净的 拉丁名-中文名 对应)
- if correlations:
- for rows in correlations.values():
- for r in rows:
- la, cn = r.get("拉丁名", ""), r.get("菌种", "")
- cn0 = re.split(r"[((]", cn)[0]
- if la and cn0 and len(cn0) <= 24:
- if la not in latin_cn or len(cn0) < len(latin_cn[la]):
- latin_cn[la] = cn0
- for group, latin_key, cn_key in (("门水平", "拉丁名", "中文名"), ("属水平", "拉丁名", "中文名"),
- ("特征菌科属种", "拉丁学名", "中文名称")):
- for r in result["肠道菌群组成"][group]:
- la, cn = r.get(latin_key) or "", r.get(cn_key) or ""
- cn0 = re.split(r"[((]", cn)[0]
- if la and cn0 and (la not in latin_cn or len(cn0) < len(latin_cn[la])):
- latin_cn[la] = cn0
- for key in ("有益菌检出", "条件致病菌检出", "致病菌检出"):
- for r in result.get(key, []):
- la = r.get("拉丁名", "")
- base = la.split()[0] if la else ""
- if la:
- dict_cn = latin_cn.get(la) or latin_cn.get(base)
- cur = r.get("中文名", "")
- # 缺失或明显溢出(如整句叙述混入)时用词典标准中文名覆盖
- if dict_cn and (not cur or len(cur) > len(dict_cn) + 2):
- r["中文名"] = dict_cn
- # ---- 5) 报告声明(致客户)----
- result["报告声明"] = "\n".join(l.strip() for l in doc[3].get_text().splitlines() if l.strip())
- return result
- def _absorb_blocks(cur_sec, blocks, correlations, disease_notes, details, ref_tables):
- """把页首无归属文本块并入上一章节。"""
- code, name, kind = cur_sec
- if kind == "disease" and (blocks.get("疾病介绍") or blocks.get("调理建议")):
- sec_name = DETAIL_TITLES.get(code, name)
- rec = disease_notes.setdefault(sec_name, {"疾病介绍": "", "调理建议": ""})
- rec["疾病介绍"] += blocks.get("疾病介绍", "")
- rec["调理建议"] += blocks.get("调理建议", "")
- elif kind == "detail":
- sec_name = DETAIL_TITLES.get(code, name)
- for entry in details:
- if entry.get("编号") == code:
- for lbl in ("相关介绍", "食物来源", "注意事项", "温馨提示", "结果提示"):
- if blocks.get(lbl):
- entry[lbl] = entry.get(lbl, "") + blocks[lbl]
- break
- def _parse_section_page(doc, pno, words, cur_sec, y_start, y_end,
- correlations, disease_notes, details, ref_tables):
- """解析单个章节在其起始页/续页上的表格与文本块。"""
- code, name, kind = cur_sec
- sec_name = DETAIL_TITLES.get(code, name)
- hi = y_end if y_end is not None else 10 ** 9
- scope_words = [w for w in words if y_start < w[1] < hi] if y_start else words
- if kind in ("allergy", "disease"):
- # 相关性表:找"菌种"表头;续页无表头时从 y_start 开始
- header_y = None
- for x, y, t in scope_words:
- if t == "菌种":
- header_y = y
- break
- t_start = header_y if header_y is not None else (y_start if y_start else 100)
- t_end = next_marker_y(words, t_start, markers=("※", "【", "疾病介绍:", "调理建议:", "第"))
- rows = parse_correlation_table(words, t_start, t_end)
- if rows:
- correlations.setdefault(sec_name, []).extend(rows)
- blocks = extract_text_blocks(words, y_start if y_start else 0, y_end)
- if kind == "disease" and (blocks.get("疾病介绍") or blocks.get("调理建议")):
- rec = disease_notes.setdefault(sec_name, {"疾病介绍": "", "调理建议": ""})
- rec["疾病介绍"] += blocks.get("疾病介绍", "")
- rec["调理建议"] += blocks.get("调理建议", "")
- return
- # kind == detail
- if code in ("1.6", "1.7", "1.7.1", "1.7.2"):
- return # 细菌表由专门流程处理
- header_y, header_kind = None, None
- for x, y, t in scope_words:
- if t in ("功能", "检测项目"):
- header_y, header_kind = y, t
- break
- entry = {"编号": code, "名称": sec_name}
- if code == "1.1":
- # GMHI 两列表:检测项目|评估值
- rows = parse_quad_table(words, y_start, y_end, cols=[(90, 300), (300, 520)])
- if rows:
- entry["表格"] = [{"检测项目": r[0], "评估值": r[1]} for r in rows if len(r) >= 2 and r[0]]
- elif header_y is not None and header_kind in ("功能", "检测项目"):
- t_end = next_marker_y(words, header_y, markers=("※", "【", "相关介绍:"))
- if header_kind == "功能":
- rows = parse_quad_table(words, header_y, t_end, cols=[(90, 175), (175, 280), (280, 400), (400, 520)])
- if rows:
- entry["表格"] = [{"功能": r[0], "评估值": r[1], "参考范围": r[2], "结果评价": r[3]}
- for r in rows if len(r) >= 4]
- else:
- rows = parse_quad_table(words, header_y, t_end, cols=[(90, 170), (170, 250), (250, 380), (380, 520)])
- if rows:
- ref_tables.setdefault(sec_name, []).extend(
- [{"检测项目": r[0], "检测值": r[1], "结果评价": r[2], "参考范围": r[3]}
- for r in rows if len(r) >= 4])
- blocks = extract_text_blocks(words, y_start if y_start else 0, y_end)
- for lbl in ("相关介绍", "食物来源", "注意事项", "温馨提示", "结果提示"):
- if blocks.get(lbl):
- entry[lbl] = blocks[lbl]
- if len(entry) > 2 or entry.get("表格") or ref_tables.get(sec_name):
- details.append(entry)
- def main():
- if len(sys.argv) < 2:
- sys.exit(__doc__)
- pdf = Path(sys.argv[1])
- out = Path(sys.argv[sys.argv.index("-o") + 1]) if "-o" in sys.argv else pdf.with_suffix(".json")
- data = parse(pdf)
- out.write_text(json.dumps(data, ensure_ascii=False, indent=1), encoding="utf-8")
- print(f"written: {out}")
- for k in ["报告概述", "肠道免疫力评估", "营养物质代谢能力", "短链脂肪酸合成能力", "维生素评估",
- "天然色素合成能力", "氨基酸合成能力", "谷胱甘肽合成能力", "胆汁酸合成能力",
- "健康相关矿物质评估", "多轴系统风险评估", "毒素降解能力评估", "抗生素耐药性评估",
- "肠道菌群过敏风险评估", "肠道菌群与疾病相关风险评估", "有益菌检出", "条件致病菌检出",
- "致病菌检出", "指标详解", "疾病相关菌", "过敏相关菌", "疾病介绍与调理建议",
- "分项检测参考", "肠道菌群组成"]:
- v = data.get(k)
- if isinstance(v, dict) and k == "肠道菌群组成":
- print(f" {k}: 门{len(v['门水平'])} 属{len(v['属水平'])} 特征菌{len(v['特征菌科属种'])}")
- else:
- n = len(v) if hasattr(v, "__len__") else "-"
- print(f" {k}: {n}")
- if __name__ == "__main__":
- main()
|