# -*- coding: utf-8 -*- """把 ch6_blocks.json 组装成可交付 Markdown:正文 + 目录 + 适配对照附录""" import json, re, os, sqlite3 BASE = r"C:\Users\MZ\AppData\Local\Temp\cos4std" BLK = os.path.join(BASE, "ch6_blocks.json") OUT = os.path.join(BASE, "final_ch6.md") DB = r"D:\PricingReview\AdminCost\data.db" # 估算/概算专属元素(本项目为计价文件,不纳入) EST = { "ConstructionSummary", "ConstructionCost", "EquipmentProcurementCost", "EquipmentProcurementCostItem", "OtherInvestmentOfConstructionProject", "OtherInvestmentOfConstructionProjectGroup", "OtherInvestmentOfConstructionProjectItem", "ContingencyFee", "ContingencyFeeGroup", "ContingencyFeeItem", "UrbanRailTransitVehicleProcurementCost", "UrbanRailTransitVehicleProcurementCostGroup", "UrbanRailTransitVehicleProcurementCostItem", "InterestDuringConstructionPeriod", "InterestDuringConstructionPeriodGroup", "InterestDuringConstructionPeriodItem", "InitialWorkingCapital", "InitialWorkingCapitalGroup", "InitialWorkingCapitalItem", "InitialWorkingCapitalltem", # OCR 把 Item 识别成 ltem } CN_TITLE_FIX = {"其他项目费明细": "SundryCostsItem", "增值稅": "Tax", "增值税": "Tax"} CN_EXTRA = { "ProvisionalMaterialEquipmentItem": "材料暂估价明细", "InitialWorkingCapitalltem": "铺底流动资金明细", "OtherCost": "扩展项", "SubdivisionFees": "构成明细", "IncFees": "子目增加费", "BidEvaluationMainMaterial": "评标主要材料", } def ptitle(t): t = re.sub(r"\s+", " ", t) m = re.match(r"^表\s*6\s*[..]\s*(\d+)\s*[..]\s*(\d+)((?:-\d+)?)\s*(.*?)属性定义表", t) if not m: return None, None return "6.%s.%s%s" % (m.group(1), m.group(2), m.group(3)), m.group(4).strip() def elem_of(rest): if rest in CN_TITLE_FIX: return CN_TITLE_FIX[rest] m = re.search(r"([A-Z][A-Za-z]{2,})", rest) return m.group(1) if m else rest def props_of(rows): out = [] for r in rows: cs = r["cells"] if len(cs) > 1: v = re.sub(r"\s+", "", cs[1]) if re.fullmatch(r"[A-Za-z][A-Za-z0-9]{2,}", v): out.append(v) return out def load_db(): c = sqlite3.connect(DB) els, props = {}, {} for r in c.execute("select distinct element from tabs where element is not null and element<>''"): els[r[0]] = 1 for r in c.execute("""select t.element el, c.std_name sn from columns c join tabs t on t.id=c.tab_id where t.element is not null and t.element<>'' and c.std_name is not null and c.std_name<>''"""): props.setdefault(r[0], set()).add(r[1].strip()) return els, props def cn_names(B): """从条文里抽 元素 -> 中文名""" m = {} for b in B: t = b.get("text") or "" for mm in re.finditer(r"([A-Za-z][A-Za-z]{2,})\s*[((]([\u4e00-\u9fa5、()\-]{2,20})[))]", t): m.setdefault(mm.group(1), mm.group(2)) for mm in re.finditer(r"([\u4e00-\u9fa5]{2,12})的元素名称\s*([A-Za-z][A-Za-z]{2,})", t): m.setdefault(mm.group(2), mm.group(1)) return m def main(): B = json.load(open(BLK, encoding="utf-8")) els, dbprops = load_db() CN = cn_names(B) # 章节 + 表清单 heads = [(re.sub(r"^(6\.[123])\d$", r"\1", b["no"]), b["text"]) for b in B if b["kind"] == "heading"] tables = [] for b in B: if b["kind"] == "table": no, rest = ptitle(b["title"] or "") tables.append((no, elem_of(rest) if rest else "", b)) lines = [] A = lines.append A("# 6 工程计价文件") A("") A("> **来源**:DBJ/T 15-145-2025《建设工程政府投资项目造价数据标准》(广东省地方标准),2025 版。") A("> **原文**:4793785.pdf(广东省住房和城乡建设厅发布),第 6 章 = PDF 第 52–146 页 / 印刷页第 44–138 页。") A("> **转换方式**:该 PDF 为纯扫描件(191 页无文字层),经 300dpi 渲染 + RapidOCR 识别,按版面坐标重建表格。") A("> **可靠性说明**:中文正文偶有形近字(如“四舍五入”识别为“四舍五保留”)、公式符号有丢失;") A("> **属性名(英文驼峰)识别准确**,可直接用作字段规范;数值精度、必填列以原文为准。") A("> 每表末尾“注:”为原文注释,OCR 后已并入正文段落。") A("") A("---") A("") A("## 目录") A("") for no, txt in heads: A("- **%s %s**" % (no, txt) if no else "- **%s**" % txt) A("- **附录 A 元素清单与适配判定**") A("") A("**属性定义表索引**(共 %d 张)" % len(tables)) A("") A("| 表号 | 元素 | 属性条数 |") A("|---|---|---|") for no, el, b in tables: A("| %s | %s | %d |" % (no or "—", el or "—", len(b["rows"]))) A("") A("---") A("") # 正文 for b in B: k = b["kind"] if k == "heading": no = b["no"] if re.fullmatch(r"6\.[123]\d", no): no = no[:3] lv = "##" if no.count(".") <= 1 else "###" A(("%s %s %s" % (lv, no, b["text"])) if no else ("## " + b["text"])) A("") elif k == "figure": A("*%s*" % b["text"]) A("") elif k == "fignote": A("> 图内文字(OCR):" + b["text"]) A("") elif k == "para": A(b["text"]) A("") elif k == "table": A("**%s**" % (b["title"] or "(续表)")) A("") cols = b.get("cols") or ["序号", "属性名称", "中文解释", "数据类型", "必填", "备注"] rows = [] for r in b["rows"]: cs = list(r["cells"]) + [""] * (len(cols) - len(r["cells"])) rows.append(cs[: len(cols)]) if cols and cols[0] == "序号" and rows: if sum(1 for c in rows if not c[0].strip()) * 2 >= len(rows): for i, c in enumerate(rows, 1): c[0] = str(i) A("| " + " | ".join(cols) + " |") A("|" + "|".join(["---"] * len(cols)) + "|") for cs in rows: cs = [x.replace("|", "/").replace("\n", " ") for x in cs] A("| " + " | ".join(cs) + " |") A("") # 附录 A A("---") A("") A("## 附录 A 元素清单与适配判定") A("") A("判定口径:本标准 6.2.1 第 3 款规定,施工图预算、招标工程量清单、最高投标限价、投标报价、") A("签约合同价、过程结算价、竣工结算价七类文件的建设项目,数据内容为**建筑安装工程费**,") A("子元素为 SystemInfo、ConstructionInfo、SummaryOfCost、ProjectInstallationWorkCost。") A("故工程费用、设备购置费、工程建设其他费用、预备费、车辆购置费、建设期利息、铺底流动资金") A("(估算/概算路径)**不纳入**;建安费路径上的全部元素**应纳入**。") A("") A("| 表号 | 元素 | 中文名 | 属性条数 | 现有结构 | 判定 |") A("|---|---|---|---|---|---|") for no, el, b in tables: have = "已有" if el in els else "缺" cn = CN.get(el) or CN_EXTRA.get(el, "") if el in EST: jud = "不适用(估算/概算)" elif el in els: jud = "已建,需按本表校属性" else: jud = "**应补建**" A("| %s | %s | %s | %d | %s | %s |" % (no or "—", el, cn, len(b["rows"]), have, jud)) A("") A("> 注:属性条数为 OCR 抽取的英文属性名行数,个别跨行排列的长属性名会被拆成两行,") A("> 故实际属性数可能比本列多 1–2 条;精确条数以正文表格为准。") A("") md = "\n".join(lines) open(OUT, "w", encoding="utf-8").write(md) print("md chars", len(md), "tables", len(tables), "heads", len(heads)) return OUT if __name__ == "__main__": print(main())