investment_parser.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. """
  3. 投资估算表格解析模块
  4. 支持三种类型:
  5. 1. 可研批复投资估算
  6. 2. 可研评审投资估算
  7. 3. 初设批复概算投资
  8. """
  9. from typing import List, Optional
  10. import re
  11. from ..utils.logging_config import get_logger
  12. from ..models.data_models import (
  13. FeasibilityApprovalInvestment,
  14. FeasibilityReviewInvestment,
  15. PreliminaryApprovalInvestment,
  16. InvestmentItem,
  17. FinalAccountRecord,
  18. FinalAccountItem
  19. )
  20. from .table_parser import extract_table_with_rowspan_colspan
  21. logger = get_logger("pdf_converter_v2.parser.investment")
  22. def detect_investment_type(markdown_content: str) -> Optional[str]:
  23. """
  24. 检测投资估算表格类型
  25. Returns:
  26. str: 类型名称
  27. - "fsApproval" - 可研批复
  28. - "fsReview" - 可研评审
  29. - "pdApproval" - 初设批复
  30. - None - 无法识别
  31. """
  32. # 检查标题关键词
  33. if "可研批复" in markdown_content or "可行性研究报告的批复" in markdown_content:
  34. # 检查是否有建设规模相关列(可研批复特有)
  35. if "架空线" in markdown_content or "间隔" in markdown_content:
  36. logger.info("[投资估算] 检测到类型: 可研批复投资估算")
  37. return "fsApproval"
  38. if "可研评审" in markdown_content or "可行性研究报告的评审意见" in markdown_content:
  39. logger.info("[投资估算] 检测到类型: 可研评审投资估算")
  40. return "fsReview"
  41. if "初设批复" in markdown_content or "初步设计的批复" in markdown_content:
  42. logger.info("[投资估算] 检测到类型: 初设批复概算投资")
  43. return "pdApproval"
  44. logger.warning("[投资估算] 无法识别投资估算表格类型")
  45. return None
  46. def determine_level(text: str, name: str = "", strict_mode: bool = True) -> str:
  47. """
  48. 判断明细等级
  49. 规则:
  50. - 大写中文数字(一、二、三等) -> 第一级(顶级大类)
  51. - strict_mode=True: 需要名称包含电压等级+输变电工程才是一级,否则降为二级
  52. - strict_mode=False: 中文数字直接判断为一级(用于 fsReview、pdApproval)
  53. - 小写阿拉伯数字(1、2、3等) -> 第二级
  54. - 带括号的数字(1)、2)等) -> 第三级
  55. - 合计 -> 0
  56. Args:
  57. text: 序号或名称文本
  58. name: 可选,名称文本,用于辅助判断(区分顶级大类和子项)
  59. strict_mode: 是否使用严格模式(默认True,用于 fsApproval 区分顶级大类)
  60. Returns:
  61. str: "0"(合计), "1"(一级), "2"(二级), "3"(三级), ""(无法判断)
  62. """
  63. if not text:
  64. return ""
  65. text = text.strip()
  66. # 合计行(包含"合 计"这种带空格的情况)
  67. text_no_space = text.replace(" ", "")
  68. if "合计" in text_no_space or "小计" in text_no_space:
  69. return "0"
  70. # 第一级: 大写中文数字
  71. # 匹配: "一、", "一,", "一.", "一 ", "一" (后面可以跟任意字符或结束)
  72. # 注意:需要排除"十一"、"十二"等多位数字,只匹配单个中文数字
  73. is_chinese_numeral = False
  74. if re.match(r'^[一二三四五六七八九十]+[、,,.\s]', text):
  75. is_chinese_numeral = True
  76. # 如果序号后面直接跟汉字(没有标点),也可能是第一级
  77. # 例如: "一变电工程", "二线路工程"
  78. elif re.match(r'^[一二三四五六七八九十]+[\u4e00-\u9fa5]', text):
  79. is_chinese_numeral = True
  80. # 如果只是单独的中文数字(没有后续字符),也可能是第一级
  81. # 例如: "一", "二", "三"
  82. elif re.match(r'^[一二三四五六七八九十]+$', text):
  83. is_chinese_numeral = True
  84. if is_chinese_numeral:
  85. # 非严格模式:中文数字直接判断为一级(用于 fsReview、pdApproval)
  86. if not strict_mode:
  87. return "1"
  88. # 严格模式:进一步判断,区分顶级大类和子项目(用于 fsApproval)
  89. # 顶级大类特征:名称包含电压等级(如"220千伏"、"500kV")+ 输变电工程
  90. # 子项目特征:简单的工程类型名称(变电工程、线路工程、配套通信工程)
  91. name_to_check = name if name else text
  92. # 1. 检查是否是顶级大类(包含电压等级 + 输变电工程)
  93. # 电压等级模式:220千伏、500kV、110kv、35千伏等
  94. has_voltage = bool(re.search(r'\d+\s*(千伏|kV|KV|kv)', name_to_check, re.IGNORECASE))
  95. has_project_type = "输变电" in name_to_check or "变电站" in name_to_check or "送出工程" in name_to_check
  96. if has_voltage and has_project_type:
  97. # 包含电压等级和工程类型,是顶级大类
  98. return "1"
  99. # 2. 检查是否是子项目(固定名称,人为错误可能把"3"写成"三")
  100. # 子项目名称通常较短且是固定的工程类型
  101. subitem_exact = ["变电工程", "线路工程", "配套通信工程", "通信工程"]
  102. is_exact_subitem = name_to_check in subitem_exact
  103. if is_exact_subitem:
  104. # 完全匹配子项目名称,按二级处理
  105. logger.debug(f"[等级判断] 中文数字序号但名称是子项目,按二级处理: text={text}, name={name}")
  106. return "2"
  107. # 3. 其他情况:如果名称较长(>10字符),可能是顶级大类;否则按二级处理
  108. if len(name_to_check) > 10:
  109. # 较长的名称可能是顶级大类(即使没有匹配到电压等级模式)
  110. return "1"
  111. else:
  112. # 较短的名称,按二级处理
  113. logger.debug(f"[等级判断] 中文数字序号但名称较短,按二级处理: text={text}, name={name}")
  114. return "2"
  115. # 第二级: 小写阿拉伯数字
  116. # 匹配: "1、", "1,", "1.", "1 " (后面跟标点或空格)
  117. if re.match(r'^\d+[、,,.\s]', text) and not text.startswith('(') and not text.startswith('('):
  118. return "2"
  119. # 如果数字后面直接跟汉字(没有标点),也认为是第二级
  120. # 例如: "1周村220kV变电站"
  121. if re.match(r'^\d+[\u4e00-\u9fa5]', text) and not text.startswith('(') and not text.startswith('('):
  122. return "2"
  123. # 如果只是单独的阿拉伯数字(没有后续字符),也是第二级
  124. # 例如: "1", "2", "3"
  125. if re.match(r'^\d+$', text) and not text.startswith('(') and not text.startswith('('):
  126. return "2"
  127. # 第三级: 带括号的数字,或者数字后跟右括号
  128. # 匹配: "(1)", "(1)", "1)", "1)"
  129. if re.match(r'^[((]\d+[))]', text):
  130. return "3"
  131. # 数字后跟右括号,如 "1)", "2)"
  132. if re.match(r'^\d+[))]', text):
  133. return "3"
  134. return ""
  135. def clean_number_string(value: str) -> str:
  136. """
  137. 清理数字字符串
  138. - 移除千位分隔符
  139. - 移除单位
  140. - 保留小数点
  141. Args:
  142. value: 原始数字字符串
  143. Returns:
  144. str: 清理后的数字字符串
  145. """
  146. if not value or not value.strip():
  147. return ""
  148. value = value.strip()
  149. # 移除常见单位
  150. value = re.sub(r'[万元元]', '', value)
  151. # 移除千位分隔符
  152. value = value.replace(',', '').replace(',', '')
  153. # 移除空格
  154. value = value.replace(' ', '')
  155. return value
  156. def parse_feasibility_approval_investment(markdown_content: str) -> FeasibilityApprovalInvestment:
  157. """
  158. 解析可研批复投资估算
  159. 包含字段:
  160. - No: 序号
  161. - name: 工程或费用名称
  162. - Level: 明细等级
  163. - constructionScaleOverheadLine: 建设规模-架空线
  164. - constructionScaleBay: 建设规模-间隔
  165. - constructionScaleSubstation: 建设规模-变电
  166. - constructionScaleOpticalCable: 建设规模-光缆
  167. - staticInvestment: 静态投资(元)
  168. - dynamicInvestment: 动态投资(元)
  169. """
  170. record = FeasibilityApprovalInvestment()
  171. tables = extract_table_with_rowspan_colspan(markdown_content)
  172. if not tables:
  173. logger.warning("[可研批复投资] 未能提取出任何表格内容")
  174. return record
  175. # 找到所有投资估算表格并合并
  176. # 因为OCR可能将一个大表格拆分成多个<table>
  177. all_matching_tables = []
  178. for table_idx, table in enumerate(tables):
  179. table_text = ""
  180. for row in table:
  181. table_text += " ".join([str(cell) for cell in row])
  182. # 移除空格后再匹配
  183. table_text_no_space = table_text.replace(" ", "")
  184. # 选择包含"工程或费用名称"或"项目名称",且包含"静态投资"或"静态合计"的表格
  185. has_name_col = ("工程或费用名称" in table_text_no_space or "项目名称" in table_text_no_space)
  186. has_investment_col = ("静态投资" in table_text_no_space or "静态合计" in table_text_no_space)
  187. if has_name_col and has_investment_col:
  188. all_matching_tables.append((table_idx, table))
  189. logger.info(f"[可研批复投资] 找到投资估算表格 (表格{table_idx+1}), 行数: {len(table)}")
  190. if not all_matching_tables:
  191. logger.warning("[可研批复投资] 未找到包含投资估算的表格")
  192. return record
  193. # 如果只有一个表格,直接使用
  194. if len(all_matching_tables) == 1:
  195. target_table = all_matching_tables[0][1]
  196. else:
  197. # 多个表格:合并所有表格的数据行(跳过重复的表头行)
  198. logger.info(f"[可研批复投资] 发现 {len(all_matching_tables)} 个投资估算表格,将进行合并")
  199. target_table = []
  200. first_table = True
  201. for table_idx, table in all_matching_tables:
  202. if first_table:
  203. # 第一个表格:保留全部内容(包括表头)
  204. target_table.extend(table)
  205. first_table = False
  206. else:
  207. # 后续表格:跳过表头行(前几行包含"序号"、"工程或费用名称"等)
  208. header_end_idx = 0
  209. for row_idx, row in enumerate(table):
  210. row_text = " ".join([str(cell) for cell in row]).replace(" ", "")
  211. # 如果这行包含表头关键词,继续跳过
  212. if "序号" in row_text or "工程或费用名称" in row_text or "建设规模" in row_text:
  213. header_end_idx = row_idx + 1
  214. # 如果第一列是中文数字(一、二、三...),说明是数据行开始
  215. elif len(row) > 0:
  216. first_cell = str(row[0]).strip()
  217. if first_cell in ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]:
  218. break
  219. # 只添加数据行
  220. target_table.extend(table[header_end_idx:])
  221. logger.debug(f"[可研批复投资] 表格{table_idx+1}: 跳过前{header_end_idx}行表头,添加{len(table)-header_end_idx}行数据")
  222. logger.info(f"[可研批复投资] 合并后总行数: {len(target_table)}")
  223. # 识别表头行和列索引
  224. # 注意:表格可能有多层表头(rowspan),需要扫描前几行来找到所有列名
  225. header_row_idx = -1
  226. no_idx = -1
  227. name_idx = -1
  228. overhead_line_idx = -1
  229. bay_idx = -1
  230. substation_idx = -1
  231. optical_cable_idx = -1
  232. static_investment_idx = -1
  233. dynamic_investment_idx = -1
  234. # 新增费用列索引
  235. construction_project_cost_idx = -1 # 建筑工程费
  236. equipment_purchase_cost_idx = -1 # 设备购置费
  237. installation_project_cost_idx = -1 # 安装工程费
  238. other_expenses_idx = -1 # 其他费用(合计)
  239. # 扫描前几行(最多5行)来识别列索引
  240. for row_idx in range(min(5, len(target_table))):
  241. row = target_table[row_idx]
  242. row_text = " ".join([str(cell) for cell in row])
  243. row_text_no_space = row_text.replace(" ", "")
  244. # 识别各列(遍历所有行的所有列)
  245. for col_idx, cell in enumerate(row):
  246. cell_text = str(cell).strip()
  247. cell_text_no_space = cell_text.replace(" ", "")
  248. if "序号" in cell_text and no_idx == -1:
  249. no_idx = col_idx
  250. elif ("工程或费用名称" in cell_text_no_space or "项目名称" in cell_text_no_space) and name_idx == -1:
  251. name_idx = col_idx
  252. elif "架空线" in cell_text_no_space and overhead_line_idx == -1:
  253. overhead_line_idx = col_idx
  254. elif "间隔" in cell_text and bay_idx == -1:
  255. bay_idx = col_idx
  256. elif "变电" in cell_text and substation_idx == -1:
  257. substation_idx = col_idx
  258. elif "光缆" in cell_text and optical_cable_idx == -1:
  259. optical_cable_idx = col_idx
  260. elif ("静态投资" in cell_text_no_space or "静态合计" in cell_text_no_space) and static_investment_idx == -1:
  261. static_investment_idx = col_idx
  262. elif ("动态投资" in cell_text_no_space or "动态合计" in cell_text_no_space) and dynamic_investment_idx == -1:
  263. dynamic_investment_idx = col_idx
  264. # 新增费用字段识别
  265. elif "建筑工程费" in cell_text_no_space and construction_project_cost_idx == -1:
  266. construction_project_cost_idx = col_idx
  267. elif "设备购置费" in cell_text_no_space and equipment_purchase_cost_idx == -1:
  268. equipment_purchase_cost_idx = col_idx
  269. elif "安装工程费" in cell_text_no_space and installation_project_cost_idx == -1:
  270. installation_project_cost_idx = col_idx
  271. elif ("其他费用" in cell_text_no_space or "合计" == cell_text_no_space) and other_expenses_idx == -1:
  272. # 其他费用列通常标题为"合计"或"其他费用"
  273. # 注意:表头可能有"合计"列在"其他费用"下面
  274. if "其他费用" in cell_text_no_space:
  275. other_expenses_idx = col_idx
  276. # 如果这一行包含"序号"或"工程或费用名称"或"项目名称",记录为表头结束行
  277. if ("序号" in row_text or "工程或费用名称" in row_text_no_space or "项目名称" in row_text_no_space) and header_row_idx == -1:
  278. header_row_idx = row_idx
  279. # 表头结束行应该是最后一个包含表头内容的行
  280. # 找到第一个数据行(通常是"一"、"二"等开头)
  281. for row_idx in range(min(5, len(target_table))):
  282. row = target_table[row_idx]
  283. if len(row) > 0:
  284. first_cell = str(row[0]).strip()
  285. # 如果第一列是中文数字或阿拉伯数字(不是"序号"),这是数据行
  286. if first_cell and first_cell not in ["序号", ""] and (first_cell in ["一", "二", "三", "四", "五"] or first_cell.isdigit()):
  287. header_row_idx = row_idx - 1
  288. logger.debug(f"[可研批复投资] 根据数据行确定表头结束于第{header_row_idx}行")
  289. break
  290. logger.info(f"[可研批复投资] 表头行: {header_row_idx}")
  291. logger.info(f"[可研批复投资] 列索引: 序号={no_idx}, 名称={name_idx}, "
  292. f"架空线={overhead_line_idx}, 间隔={bay_idx}, 变电={substation_idx}, "
  293. f"光缆={optical_cable_idx}, 静态投资={static_investment_idx}, 动态投资={dynamic_investment_idx}")
  294. logger.info(f"[可研批复投资] 费用列索引: 建筑工程费={construction_project_cost_idx}, "
  295. f"设备购置费={equipment_purchase_cost_idx}, 安装工程费={installation_project_cost_idx}, "
  296. f"其他费用={other_expenses_idx}")
  297. if header_row_idx == -1:
  298. logger.warning("[可研批复投资] 未找到表头行")
  299. return record
  300. # 解析数据行(输出全部数据,不再只筛选"四"区域)
  301. for row_idx in range(header_row_idx + 1, len(target_table)):
  302. row = target_table[row_idx]
  303. if len(row) < 3:
  304. continue
  305. # 检查是否是有效数据行(至少有名称)
  306. if name_idx >= 0 and name_idx < len(row):
  307. name = str(row[name_idx]).strip()
  308. if not name or name in ["", "nan", "None"]:
  309. continue
  310. # 提取序号
  311. no = ""
  312. if no_idx >= 0 and no_idx < len(row):
  313. no = str(row[no_idx]).strip()
  314. # 判断等级,传入 name 辅助区分顶级大类和子项
  315. level_input = (no + name) if no else name
  316. level = determine_level(level_input, name)
  317. item = InvestmentItem()
  318. item.no = no
  319. item.name = name
  320. item.level = level
  321. # 提取建设规模
  322. if overhead_line_idx >= 0 and overhead_line_idx < len(row):
  323. item.constructionScaleOverheadLine = str(row[overhead_line_idx]).strip()
  324. if bay_idx >= 0 and bay_idx < len(row):
  325. item.constructionScaleBay = str(row[bay_idx]).strip()
  326. if substation_idx >= 0 and substation_idx < len(row):
  327. item.constructionScaleSubstation = str(row[substation_idx]).strip()
  328. if optical_cable_idx >= 0 and optical_cable_idx < len(row):
  329. item.constructionScaleOpticalCable = str(row[optical_cable_idx]).strip()
  330. # 提取投资金额
  331. if static_investment_idx >= 0 and static_investment_idx < len(row):
  332. item.staticInvestment = clean_number_string(str(row[static_investment_idx]))
  333. if dynamic_investment_idx >= 0 and dynamic_investment_idx < len(row):
  334. item.dynamicInvestment = clean_number_string(str(row[dynamic_investment_idx]))
  335. # 提取费用明细
  336. if construction_project_cost_idx >= 0 and construction_project_cost_idx < len(row):
  337. item.constructionProjectCost = clean_number_string(str(row[construction_project_cost_idx]))
  338. if equipment_purchase_cost_idx >= 0 and equipment_purchase_cost_idx < len(row):
  339. item.equipmentPurchaseCost = clean_number_string(str(row[equipment_purchase_cost_idx]))
  340. if installation_project_cost_idx >= 0 and installation_project_cost_idx < len(row):
  341. item.installationProjectCost = clean_number_string(str(row[installation_project_cost_idx]))
  342. if other_expenses_idx >= 0 and other_expenses_idx < len(row):
  343. item.otherExpenses = clean_number_string(str(row[other_expenses_idx]))
  344. record.items.append(item)
  345. logger.info(f"[可研批复投资] 解析到数据: No={item.no}, Name={item.name}, Level={item.level}")
  346. logger.info(f"[可研批复投资] 共解析到 {len(record.items)} 条数据")
  347. return record
  348. def parse_safety_feasibility_approval_investment(markdown_content: str) -> FeasibilityApprovalInvestment:
  349. """
  350. 解析安全可研批复投资估算(湖北省格式)
  351. 特点:
  352. - 没有顶层大类(Level=1),直接从二级分类开始
  353. - 中文序号(一、二)表示二级分类(如"变电工程"、"线路工程")
  354. - 阿拉伯数字(1、2、3)表示具体项目
  355. - 列名使用"项目名称"和"静态合计/动态合计"
  356. 返回结构:
  357. - Level 1: 二级分类(如"变电工程"、"线路工程")
  358. - Level 2: 具体项目(如"襄阳连云220千伏变电站新建工程")
  359. """
  360. record = FeasibilityApprovalInvestment()
  361. tables = extract_table_with_rowspan_colspan(markdown_content)
  362. if not tables:
  363. logger.warning("[安全可研批复投资] 未能提取出任何表格内容")
  364. return record
  365. # 首先尝试提取项目基本信息表格
  366. for table_idx, table in enumerate(tables):
  367. if len(table) < 2:
  368. continue
  369. table_text = ""
  370. for row in table:
  371. table_text += " ".join([str(cell) for cell in row])
  372. table_text_no_space = table_text.replace(" ", "").replace("(", "(").replace(")", ")")
  373. # 查找包含"工程(项目)名称"的表格
  374. if "工程(项目)名称" in table_text_no_space or "工程项目名称" in table_text_no_space:
  375. logger.info(f"[安全可研批复投资] 找到项目信息表格 (表格{table_idx+1})")
  376. # 提取项目信息
  377. for row in table:
  378. if len(row) >= 2:
  379. key = str(row[0]).strip()
  380. value = str(row[1]).strip() if len(row) > 1 else ""
  381. if "工程" in key and "名称" in key:
  382. record.projectName = value
  383. logger.info(f"[安全可研批复投资] 提取工程名称: {value}")
  384. elif "项目单位" in key:
  385. record.projectUnit = value
  386. logger.info(f"[安全可研批复投资] 提取项目单位: {value}")
  387. elif "设计单位" in key:
  388. record.designUnit = value
  389. logger.info(f"[安全可研批复投资] 提取设计单位: {value}")
  390. break
  391. # 找到所有投资估算表格并合并
  392. all_matching_tables = []
  393. for table_idx, table in enumerate(tables):
  394. table_text = ""
  395. for row in table:
  396. table_text += " ".join([str(cell) for cell in row])
  397. table_text_no_space = table_text.replace(" ", "")
  398. # 选择包含"项目名称"且包含"静态合计"或"静态投资"的表格
  399. has_name_col = "项目名称" in table_text_no_space
  400. has_investment_col = ("静态合计" in table_text_no_space or "静态投资" in table_text_no_space)
  401. if has_name_col and has_investment_col:
  402. all_matching_tables.append((table_idx, table))
  403. logger.info(f"[安全可研批复投资] 找到投资估算表格 (表格{table_idx+1}), 行数: {len(table)}")
  404. if not all_matching_tables:
  405. logger.warning("[安全可研批复投资] 未找到包含投资估算的表格")
  406. return record
  407. # 如果只有一个表格,直接使用
  408. if len(all_matching_tables) == 1:
  409. target_table = all_matching_tables[0][1]
  410. else:
  411. # 多个表格:合并所有表格的数据行
  412. logger.info(f"[安全可研批复投资] 发现 {len(all_matching_tables)} 个投资估算表格,将进行合并")
  413. target_table = []
  414. first_table = True
  415. for table_idx, table in all_matching_tables:
  416. if first_table:
  417. target_table.extend(table)
  418. first_table = False
  419. else:
  420. # 跳过表头行
  421. header_end_idx = 0
  422. for row_idx, row in enumerate(table):
  423. row_text = " ".join([str(cell) for cell in row]).replace(" ", "")
  424. if "序号" in row_text or "项目名称" in row_text or "建设规模" in row_text:
  425. header_end_idx = row_idx + 1
  426. elif len(row) > 0:
  427. first_cell = str(row[0]).strip()
  428. if first_cell in ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]:
  429. break
  430. target_table.extend(table[header_end_idx:])
  431. logger.debug(f"[安全可研批复投资] 表格{table_idx+1}: 跳过前{header_end_idx}行表头,添加{len(table)-header_end_idx}行数据")
  432. logger.info(f"[安全可研批复投资] 合并后总行数: {len(target_table)}")
  433. # 识别表头行和列索引
  434. header_row_idx = -1
  435. no_idx = -1
  436. name_idx = -1
  437. overhead_line_idx = -1
  438. bay_idx = -1
  439. substation_idx = -1
  440. optical_cable_idx = -1
  441. static_investment_idx = -1
  442. dynamic_investment_idx = -1
  443. construction_project_cost_idx = -1
  444. equipment_purchase_cost_idx = -1
  445. installation_project_cost_idx = -1
  446. other_expenses_idx = -1
  447. # 扫描前几行识别列索引
  448. for row_idx in range(min(5, len(target_table))):
  449. row = target_table[row_idx]
  450. row_text = " ".join([str(cell) for cell in row])
  451. row_text_no_space = row_text.replace(" ", "")
  452. for col_idx, cell in enumerate(row):
  453. cell_text = str(cell).strip()
  454. cell_text_no_space = cell_text.replace(" ", "")
  455. if "序号" in cell_text and no_idx == -1:
  456. no_idx = col_idx
  457. elif "项目名称" in cell_text_no_space and name_idx == -1:
  458. name_idx = col_idx
  459. elif "架空线" in cell_text_no_space and overhead_line_idx == -1:
  460. overhead_line_idx = col_idx
  461. elif "间隔" in cell_text and bay_idx == -1:
  462. bay_idx = col_idx
  463. elif "变电" in cell_text and substation_idx == -1:
  464. substation_idx = col_idx
  465. elif "光缆" in cell_text and optical_cable_idx == -1:
  466. optical_cable_idx = col_idx
  467. elif ("静态投资" in cell_text_no_space or "静态合计" in cell_text_no_space) and static_investment_idx == -1:
  468. static_investment_idx = col_idx
  469. elif ("动态投资" in cell_text_no_space or "动态合计" in cell_text_no_space) and dynamic_investment_idx == -1:
  470. dynamic_investment_idx = col_idx
  471. elif "建筑工程费" in cell_text_no_space and construction_project_cost_idx == -1:
  472. construction_project_cost_idx = col_idx
  473. elif "设备购置费" in cell_text_no_space and equipment_purchase_cost_idx == -1:
  474. equipment_purchase_cost_idx = col_idx
  475. elif "安装工程费" in cell_text_no_space and installation_project_cost_idx == -1:
  476. installation_project_cost_idx = col_idx
  477. elif ("其他费用" in cell_text_no_space or "合计" == cell_text_no_space) and other_expenses_idx == -1:
  478. if "其他费用" in cell_text_no_space:
  479. other_expenses_idx = col_idx
  480. if ("序号" in row_text or "项目名称" in row_text_no_space) and header_row_idx == -1:
  481. header_row_idx = row_idx
  482. # 找到第一个数据行
  483. for row_idx in range(min(5, len(target_table))):
  484. row = target_table[row_idx]
  485. if len(row) > 0:
  486. first_cell = str(row[0]).strip()
  487. if first_cell and first_cell not in ["序号", ""] and (first_cell in ["一", "二", "三", "四", "五"] or first_cell.isdigit()):
  488. header_row_idx = row_idx - 1
  489. logger.debug(f"[安全可研批复投资] 根据数据行确定表头结束于第{header_row_idx}行")
  490. break
  491. logger.info(f"[安全可研批复投资] 表头行: {header_row_idx}")
  492. logger.info(f"[安全可研批复投资] 列索引: 序号={no_idx}, 名称={name_idx}, "
  493. f"架空线={overhead_line_idx}, 间隔={bay_idx}, 变电={substation_idx}, "
  494. f"光缆={optical_cable_idx}, 静态投资={static_investment_idx}, 动态投资={dynamic_investment_idx}")
  495. if header_row_idx == -1:
  496. logger.warning("[安全可研批复投资] 未找到表头行")
  497. return record
  498. # 解析数据行
  499. for row_idx in range(header_row_idx + 1, len(target_table)):
  500. row = target_table[row_idx]
  501. if len(row) < 3:
  502. continue
  503. # 检查是否是有效数据行
  504. if name_idx >= 0 and name_idx < len(row):
  505. name = str(row[name_idx]).strip()
  506. if not name or name in ["", "nan", "None"]:
  507. continue
  508. # 提取序号
  509. no = ""
  510. if no_idx >= 0 and no_idx < len(row):
  511. no = str(row[no_idx]).strip()
  512. # 判断等级 - 使用非严格模式,让中文数字直接返回 Level 1
  513. level_input = (no + name) if no else name
  514. level = determine_level(level_input, name, strict_mode=False)
  515. # 对于阿拉伯数字序号,如果当前是 Level 2,且不是具体项目名称,则判定为 Level 2
  516. # 这样"1、襄阳连云..."会是 Level 2
  517. if level == "2" and no.isdigit():
  518. # 阿拉伯数字序号,是具体项目,保持 Level 2
  519. pass
  520. item = InvestmentItem()
  521. item.no = no
  522. item.name = name
  523. item.level = level
  524. # 提取建设规模
  525. if overhead_line_idx >= 0 and overhead_line_idx < len(row):
  526. item.constructionScaleOverheadLine = str(row[overhead_line_idx]).strip()
  527. if bay_idx >= 0 and bay_idx < len(row):
  528. item.constructionScaleBay = str(row[bay_idx]).strip()
  529. if substation_idx >= 0 and substation_idx < len(row):
  530. item.constructionScaleSubstation = str(row[substation_idx]).strip()
  531. if optical_cable_idx >= 0 and optical_cable_idx < len(row):
  532. item.constructionScaleOpticalCable = str(row[optical_cable_idx]).strip()
  533. # 提取投资金额
  534. if static_investment_idx >= 0 and static_investment_idx < len(row):
  535. item.staticInvestment = str(row[static_investment_idx]).strip()
  536. if dynamic_investment_idx >= 0 and dynamic_investment_idx < len(row):
  537. item.dynamicInvestment = str(row[dynamic_investment_idx]).strip()
  538. # 提取费用
  539. if construction_project_cost_idx >= 0 and construction_project_cost_idx < len(row):
  540. item.constructionProjectCost = str(row[construction_project_cost_idx]).strip()
  541. if equipment_purchase_cost_idx >= 0 and equipment_purchase_cost_idx < len(row):
  542. item.equipmentPurchaseCost = str(row[equipment_purchase_cost_idx]).strip()
  543. if installation_project_cost_idx >= 0 and installation_project_cost_idx < len(row):
  544. item.installationProjectCost = str(row[installation_project_cost_idx]).strip()
  545. if other_expenses_idx >= 0 and other_expenses_idx < len(row):
  546. item.otherExpenses = str(row[other_expenses_idx]).strip()
  547. record.items.append(item)
  548. logger.info(f"[安全可研批复投资] 解析到数据: No={item.no}, Name={item.name}, Level={item.level}")
  549. logger.info(f"[安全可研批复投资] 共解析到 {len(record.items)} 条数据")
  550. return record
  551. def parse_feasibility_review_investment(markdown_content: str) -> FeasibilityReviewInvestment:
  552. """
  553. 解析可研评审投资估算
  554. 包含字段:
  555. - No: 序号
  556. - name: 工程或费用名称
  557. - Level: 明细等级
  558. - staticInvestment: 静态投资(元)
  559. - dynamicInvestment: 动态投资(元)
  560. 注意:文档中可能包含多个表格,只解析"输变电工程建设规模及投资估算表"
  561. 排除"总估算表"类型的表格
  562. """
  563. record = FeasibilityReviewInvestment()
  564. # 使用正则表达式查找表格及其前面的标题
  565. # 查找 "输变电工程" + "投资估算表" 的标题,排除 "总估算表"
  566. import re
  567. # 找到目标表格的标题位置
  568. # 标题格式如: # 山西晋城周村220kV输变电工程建设规模及投资估算表
  569. target_table_pattern = re.compile(
  570. r'#\s*[^#\n]*?(输变电工程|输变电|变电工程)[^#\n]*?(建设规模及)?投资估算表',
  571. re.IGNORECASE
  572. )
  573. # 排除"总估算表"的模式
  574. exclude_pattern = re.compile(r'总估算表', re.IGNORECASE)
  575. # 查找所有匹配的标题
  576. target_title_match = None
  577. for match in target_table_pattern.finditer(markdown_content):
  578. title_text = match.group(0)
  579. if not exclude_pattern.search(title_text):
  580. target_title_match = match
  581. logger.info(f"[可研评审投资] 找到目标表格标题: {title_text}")
  582. break
  583. if not target_title_match:
  584. logger.warning("[可研评审投资] 未找到'输变电工程投资估算表'标题")
  585. # 回退到原有逻辑
  586. tables = extract_table_with_rowspan_colspan(markdown_content)
  587. if not tables:
  588. logger.warning("[可研评审投资] 未能提取出任何表格内容")
  589. return record
  590. target_table = None
  591. for table in tables:
  592. for row in table:
  593. row_text = " ".join([str(cell) for cell in row])
  594. row_text_no_space = row_text.replace(" ", "")
  595. if "工程或费用名称" in row_text_no_space or ("序号" in row_text and "静态投资" in row_text_no_space):
  596. target_table = table
  597. logger.info(f"[可研评审投资] 回退: 找到投资估算表格, 行数: {len(table)}")
  598. break
  599. if target_table:
  600. break
  601. if not target_table:
  602. logger.warning("[可研评审投资] 未找到包含投资估算的表格")
  603. return record
  604. else:
  605. # 提取标题后面到下一个标题之间的内容(包含目标表格)
  606. title_end = target_title_match.end()
  607. # 找到下一个标题或文档结束
  608. next_title_pattern = re.compile(r'\n#\s+[^#]')
  609. next_title_match = next_title_pattern.search(markdown_content, title_end)
  610. if next_title_match:
  611. section_content = markdown_content[target_title_match.start():next_title_match.start()]
  612. else:
  613. section_content = markdown_content[target_title_match.start():]
  614. logger.debug(f"[可研评审投资] 提取表格区域内容长度: {len(section_content)} 字符")
  615. # 从该区域提取表格
  616. tables = extract_table_with_rowspan_colspan(section_content)
  617. if not tables:
  618. logger.warning("[可研评审投资] 目标区域未能提取出任何表格内容")
  619. return record
  620. # 选择第一个有效表格
  621. target_table = None
  622. for table in tables:
  623. for row in table:
  624. row_text = " ".join([str(cell) for cell in row])
  625. row_text_no_space = row_text.replace(" ", "")
  626. if "工程或费用名称" in row_text_no_space or ("序号" in row_text and "静态投资" in row_text_no_space):
  627. target_table = table
  628. logger.info(f"[可研评审投资] 找到目标投资估算表格, 行数: {len(table)}")
  629. break
  630. if target_table:
  631. break
  632. if not target_table:
  633. logger.warning("[可研评审投资] 目标区域未找到包含投资估算的表格")
  634. return record
  635. # 识别表头行和列索引(多行表头处理)
  636. # 这个表格有多行表头(rowspan/colspan),需要扫描前几行来找到所有列索引
  637. no_idx = -1
  638. name_idx = -1
  639. static_investment_idx = -1
  640. dynamic_investment_idx = -1
  641. header_row_idx = -1
  642. # 扫描前5行查找列索引
  643. scan_rows = min(5, len(target_table))
  644. for row_idx in range(scan_rows):
  645. row = target_table[row_idx]
  646. for col_idx, cell in enumerate(row):
  647. cell_text = str(cell).strip()
  648. cell_text_no_space = cell_text.replace(" ", "")
  649. if "序号" in cell_text and no_idx == -1:
  650. no_idx = col_idx
  651. elif ("工程或费用名称" in cell_text_no_space or "工程名称" in cell_text_no_space) and name_idx == -1:
  652. name_idx = col_idx
  653. elif "静态投资" in cell_text_no_space and static_investment_idx == -1:
  654. static_investment_idx = col_idx
  655. elif "动态投资" in cell_text_no_space and dynamic_investment_idx == -1:
  656. dynamic_investment_idx = col_idx
  657. logger.info(f"[可研评审投资] 列索引: 序号={no_idx}, 名称={name_idx}, "
  658. f"静态投资={static_investment_idx}, 动态投资={dynamic_investment_idx}")
  659. # 确定表头结束行(第一个数据行的前一行)
  660. # 数据行特征:第一列是中文数字(一、二、三)或阿拉伯数字
  661. for row_idx in range(len(target_table)):
  662. row = target_table[row_idx]
  663. if len(row) > 0:
  664. first_cell = str(row[0]).strip()
  665. # 检查是否是数据行(以中文数字或阿拉伯数字开头)
  666. if re.match(r'^[一二三四五六七八九十]+$', first_cell) or re.match(r'^\d+$', first_cell):
  667. # 排除表头行(检查第二列是否是表头关键词)
  668. if len(row) > 1:
  669. second_cell = str(row[1]).strip().replace(" ", "")
  670. if second_cell not in ["工程或费用名称", "工程名称", "名称", ""]:
  671. header_row_idx = row_idx - 1
  672. logger.debug(f"[可研评审投资] 确定表头结束行: 第{header_row_idx}行")
  673. break
  674. if header_row_idx == -1:
  675. header_row_idx = 2 # 默认假设前3行是表头
  676. logger.debug(f"[可研评审投资] 使用默认表头结束行: 第{header_row_idx}行")
  677. # 解析数据行
  678. for row_idx in range(header_row_idx + 1, len(target_table)):
  679. row = target_table[row_idx]
  680. if len(row) < 2:
  681. continue
  682. if name_idx >= 0 and name_idx < len(row):
  683. name = str(row[name_idx]).strip()
  684. if not name or name in ["", "nan", "None"]:
  685. continue
  686. # 跳过重复的表头行
  687. name_no_space = name.replace(" ", "")
  688. if name_no_space in ["工程或费用名称", "工程名称", "名称"]:
  689. logger.debug(f"[可研评审投资] 跳过表头行: {name}")
  690. continue
  691. item = InvestmentItem()
  692. if no_idx >= 0 and no_idx < len(row):
  693. item.no = str(row[no_idx]).strip()
  694. # 跳过表头中的序号列
  695. if item.no == "序号":
  696. continue
  697. item.name = name
  698. # 判断等级 - 使用 no 和 name 分别判断
  699. # fsReview 使用非严格模式,中文数字直接判断为一级
  700. if item.no:
  701. # 优先使用 no 判断等级
  702. item.level = determine_level(item.no, item.name, strict_mode=False)
  703. if not item.level:
  704. # 如果 no 没有匹配,尝试使用 name
  705. item.level = determine_level(item.name, item.name, strict_mode=False)
  706. else:
  707. item.level = determine_level(item.name, item.name, strict_mode=False)
  708. # 提取投资金额
  709. if static_investment_idx >= 0 and static_investment_idx < len(row):
  710. item.staticInvestment = clean_number_string(str(row[static_investment_idx]))
  711. if dynamic_investment_idx >= 0 and dynamic_investment_idx < len(row):
  712. item.dynamicInvestment = clean_number_string(str(row[dynamic_investment_idx]))
  713. record.items.append(item)
  714. logger.info(f"[可研评审投资] 解析到数据: No={item.no}, Name={item.name}, Level={item.level}, "
  715. f"静态投资={item.staticInvestment}, 动态投资={item.dynamicInvestment}")
  716. logger.info(f"[可研评审投资] 共解析到 {len(record.items)} 条数据")
  717. return record
  718. def parse_preliminary_approval_investment(markdown_content: str) -> PreliminaryApprovalInvestment:
  719. """
  720. 解析初设批复概算投资
  721. 包含字段:
  722. - No: 序号
  723. - name: 工程名称
  724. - Level: 明细等级
  725. - staticInvestment: 静态投资(元)
  726. - dynamicInvestment: 动态投资(元)
  727. Note: 需要包含合计行,合计的level为0
  728. """
  729. logger.info("[初设批复投资] ========== 开始解析初设批复概算投资 ==========")
  730. logger.debug(f"[初设批复投资] Markdown内容长度: {len(markdown_content)} 字符")
  731. record = PreliminaryApprovalInvestment()
  732. logger.info("[初设批复投资] 开始提取表格...")
  733. tables = extract_table_with_rowspan_colspan(markdown_content)
  734. logger.info(f"[初设批复投资] 提取到 {len(tables) if tables else 0} 个表格")
  735. if not tables:
  736. logger.warning("[初设批复投资] 未能提取出任何表格内容")
  737. return record
  738. # 找到包含投资估算的表格
  739. logger.info("[初设批复投资] 开始查找投资估算表格...")
  740. target_table = None
  741. for table_idx, table in enumerate(tables):
  742. logger.debug(f"[初设批复投资] 检查表格 {table_idx + 1}/{len(tables)}, 行数: {len(table)}")
  743. for row_idx, row in enumerate(table):
  744. row_text = " ".join([str(cell) for cell in row])
  745. # 移除空格后再匹配,以处理OCR可能产生的空格
  746. row_text_no_space = row_text.replace(" ", "")
  747. # 输出前几行用于调试
  748. if row_idx < 3:
  749. logger.debug(f"[初设批复投资] 表格{table_idx+1} 第{row_idx+1}行: {row_text[:100]}")
  750. if "工程名称" in row_text_no_space or ("序号" in row_text and "静态投资" in row_text_no_space):
  751. target_table = table
  752. logger.info(f"[初设批复投资] ✓ 找到投资估算表格 (表格{table_idx+1}), 行数: {len(table)}")
  753. logger.debug(f"[初设批复投资] 匹配行内容: {row_text}")
  754. break
  755. if target_table:
  756. break
  757. if not target_table:
  758. logger.warning("[初设批复投资] ✗ 未找到包含投资估算的表格")
  759. logger.warning("[初设批复投资] 查找条件: 包含'工程名称' 或 ('序号' 且 '静态投资')")
  760. return record
  761. # 识别表头行和列索引
  762. logger.info("[初设批复投资] 开始识别表头行和列索引...")
  763. header_row_idx = -1
  764. no_idx = -1
  765. name_idx = -1
  766. static_investment_idx = -1
  767. dynamic_investment_idx = -1
  768. for row_idx, row in enumerate(target_table):
  769. row_text = " ".join([str(cell) for cell in row])
  770. # 移除空格后再匹配,以处理OCR可能产生的空格
  771. row_text_no_space = row_text.replace(" ", "")
  772. logger.debug(f"[初设批复投资] 检查第{row_idx}行: {row_text[:80]}")
  773. if "工程名称" in row_text_no_space or "序号" in row_text:
  774. header_row_idx = row_idx
  775. logger.info(f"[初设批复投资] ✓ 找到表头行: 第{row_idx}行")
  776. logger.debug(f"[初设批复投资] 表头内容: {row}")
  777. for col_idx, cell in enumerate(row):
  778. cell_text = str(cell).strip()
  779. # 移除空格后再匹配,以处理OCR可能产生的空格
  780. cell_text_no_space = cell_text.replace(" ", "")
  781. logger.debug(f"[初设批复投资] 列{col_idx}: '{cell_text}' (去空格: '{cell_text_no_space}')")
  782. if "序号" in cell_text:
  783. no_idx = col_idx
  784. logger.debug(f"[初设批复投资] → 序号列: {col_idx}")
  785. elif "工程名称" in cell_text_no_space or "名称" in cell_text:
  786. name_idx = col_idx
  787. logger.debug(f"[初设批复投资] → 名称列: {col_idx}")
  788. elif "静态投资" in cell_text_no_space:
  789. static_investment_idx = col_idx
  790. logger.debug(f"[初设批复投资] → 静态投资列: {col_idx}")
  791. elif "动态投资" in cell_text_no_space:
  792. dynamic_investment_idx = col_idx
  793. logger.debug(f"[初设批复投资] → 动态投资列: {col_idx}")
  794. logger.info(f"[初设批复投资] ✓ 列索引识别完成: 序号={no_idx}, 名称={name_idx}, "
  795. f"静态投资={static_investment_idx}, 动态投资={dynamic_investment_idx}")
  796. break
  797. if header_row_idx == -1:
  798. logger.warning("[初设批复投资] ✗ 未找到表头行")
  799. logger.warning("[初设批复投资] 查找条件: 包含'工程名称' 或 '序号'")
  800. return record
  801. # 解析数据行
  802. logger.info(f"[初设批复投资] 开始解析数据行 (从第{header_row_idx + 1}行到第{len(target_table)}行)...")
  803. parsed_count = 0
  804. skipped_count = 0
  805. for row_idx in range(header_row_idx + 1, len(target_table)):
  806. row = target_table[row_idx]
  807. logger.debug(f"[初设批复投资] 处理第{row_idx}行, 列数: {len(row)}")
  808. if len(row) < 2:
  809. logger.debug(f"[初设批复投资] 跳过第{row_idx}行: 列数不足 ({len(row)} < 2)")
  810. skipped_count += 1
  811. continue
  812. if name_idx >= 0 and name_idx < len(row):
  813. name = str(row[name_idx]).strip()
  814. logger.debug(f"[初设批复投资] 第{row_idx}行名称: '{name}'")
  815. if not name or name in ["", "nan", "None"]:
  816. logger.debug(f"[初设批复投资] 跳过第{row_idx}行: 名称为空")
  817. skipped_count += 1
  818. continue
  819. item = InvestmentItem()
  820. if no_idx >= 0 and no_idx < len(row):
  821. item.no = str(row[no_idx]).strip()
  822. item.name = name
  823. # 判断等级 - pdApproval 使用非严格模式,中文数字直接判断为一级
  824. level_input = (item.no + item.name) if item.no else item.name
  825. item.level = determine_level(level_input, item.name, strict_mode=False)
  826. logger.debug(f"[初设批复投资] 等级判断: '{level_input}' -> Level={item.level}")
  827. # 提取投资金额
  828. if static_investment_idx >= 0 and static_investment_idx < len(row):
  829. raw_static = str(row[static_investment_idx])
  830. item.staticInvestment = clean_number_string(raw_static)
  831. logger.debug(f"[初设批复投资] 静态投资: '{raw_static}' -> '{item.staticInvestment}'")
  832. if dynamic_investment_idx >= 0 and dynamic_investment_idx < len(row):
  833. raw_dynamic = str(row[dynamic_investment_idx])
  834. item.dynamicInvestment = clean_number_string(raw_dynamic)
  835. logger.debug(f"[初设批复投资] 动态投资: '{raw_dynamic}' -> '{item.dynamicInvestment}'")
  836. record.items.append(item)
  837. parsed_count += 1
  838. logger.info(f"[初设批复投资] ✓ 解析到数据 #{parsed_count}: No={item.no}, Name={item.name}, Level={item.level}, "
  839. f"静态={item.staticInvestment}, 动态={item.dynamicInvestment}")
  840. else:
  841. logger.debug(f"[初设批复投资] 跳过第{row_idx}行: name_idx={name_idx} 超出范围 (行长度={len(row)})")
  842. skipped_count += 1
  843. logger.info(f"[初设批复投资] ========== 解析完成 ==========")
  844. logger.info(f"[初设批复投资] 成功解析: {parsed_count} 条")
  845. logger.info(f"[初设批复投资] 跳过: {skipped_count} 条")
  846. logger.info(f"[初设批复投资] 总计: {len(record.items)} 条数据")
  847. return record
  848. def parse_investment_record(markdown_content: str, investment_type: Optional[str] = None):
  849. """
  850. 解析投资估算记录(统一入口)
  851. Args:
  852. markdown_content: Markdown内容
  853. investment_type: 投资类型(可选,如果不提供则自动检测)
  854. - "fsApproval" - 可研批复
  855. - "fsReview" - 可研评审
  856. - "pdApproval" - 初设批复
  857. Returns:
  858. 解析后的记录对象
  859. """
  860. logger.info("=" * 80)
  861. logger.info("[投资估算] 开始解析投资估算记录")
  862. logger.info(f"[投资估算] Markdown内容长度: {len(markdown_content)} 字符")
  863. # 如果没有指定类型,自动检测
  864. if not investment_type:
  865. logger.info("[投资估算] 未指定类型,开始自动检测...")
  866. investment_type = detect_investment_type(markdown_content)
  867. logger.info(f"[投资估算] 自动检测结果: {investment_type}")
  868. else:
  869. logger.info(f"[投资估算] 指定类型: {investment_type}")
  870. if not investment_type:
  871. logger.error("[投资估算] 无法识别投资估算类型")
  872. logger.error(f"[投资估算] Markdown前500字符: {markdown_content[:500]}")
  873. return None
  874. # 根据类型调用对应的解析函数
  875. logger.info(f"[投资估算] 调用解析函数: {investment_type}")
  876. result = None
  877. if investment_type == "fsApproval":
  878. result = parse_feasibility_approval_investment(markdown_content)
  879. elif investment_type == "safetyFsApproval":
  880. # safetyFsApproval 使用独立的解析逻辑(湖北省格式)
  881. result = parse_safety_feasibility_approval_investment(markdown_content)
  882. elif investment_type == "fsReview":
  883. result = parse_feasibility_review_investment(markdown_content)
  884. elif investment_type == "pdApproval":
  885. result = parse_preliminary_approval_investment(markdown_content)
  886. else:
  887. logger.error(f"[投资估算] 未知的投资估算类型: {investment_type}")
  888. return None
  889. if result:
  890. logger.info(f"[投资估算] 解析完成,返回对象类型: {type(result).__name__}")
  891. logger.info(f"[投资估算] 记录数量: {len(result.items)}")
  892. else:
  893. logger.error("[投资估算] 解析函数返回 None")
  894. logger.info("=" * 80)
  895. return result
  896. def parse_final_account_record(markdown_content: str) -> Optional[FinalAccountRecord]:
  897. """
  898. 解析决算报告中的单项工程投资完成情况表格
  899. 从OCR输出的Markdown中提取表格数据:
  900. - 表格结构:费用项目 | 概算金额 | 决算金额(审定-不含税) | 增值税额 | 超节支金额 | 超节支率
  901. - 需要提取4个单项工程的投资完成情况
  902. Args:
  903. markdown_content: OCR转换后的Markdown内容
  904. Returns:
  905. FinalAccountRecord 对象,包含所有单项工程的费用明细
  906. """
  907. logger.info("=" * 80)
  908. logger.info("[决算报告] 开始解析决算报告")
  909. logger.info(f"[决算报告] Markdown内容长度: {len(markdown_content)} 字符")
  910. record = FinalAccountRecord()
  911. # 使用正则表达式提取单项工程名称和对应的表格
  912. # 匹配模式:数字序号 + 工程名称(在"单项工程的投资完成情况"章节内)
  913. project_patterns = [
  914. # 匹配 "1、周村 220kV 输变电工程变电站新建工程" 格式
  915. (r'(\d+)[、\..]\s*(.+?(?:工程|扩建))(?:\n|$)', 1),
  916. # 匹配 "# 1、周村220kV变电站新建工程" 格式(带标题标记)
  917. (r'#\s*(\d+)[、\..]\s*(.+?(?:工程|扩建))(?:\n|$)', 2),
  918. ]
  919. # 找到"单项工程的投资完成情况"章节的起始位置
  920. section_start = 0
  921. section_patterns = [
  922. r'单项工程的?(?:投资)?完成情况',
  923. r'#\s*单项工程',
  924. ]
  925. for pattern in section_patterns:
  926. match = re.search(pattern, markdown_content)
  927. if match:
  928. section_start = match.start()
  929. logger.info(f"[决算报告] 找到单项工程章节起始位置: {section_start}")
  930. break
  931. # 找到所有项目标题及其位置
  932. project_positions = []
  933. for pattern, priority in project_patterns:
  934. for match in re.finditer(pattern, markdown_content):
  935. # 只处理单项工程章节内的项目
  936. if match.start() < section_start:
  937. continue
  938. project_no = int(match.group(1))
  939. project_name = match.group(2).strip()
  940. # 清理项目名称中的多余空格和特殊字符
  941. project_name = re.sub(r'\s+', '', project_name)
  942. project_name = re.sub(r'\\[()\[\]]', '', project_name)
  943. # 清理LaTeX数学公式格式
  944. project_name = re.sub(r'\\mathrm\{([^}]+)\}', r'\1', project_name)
  945. project_name = re.sub(r'\\[a-zA-Z]+', '', project_name)
  946. project_positions.append({
  947. "no": project_no,
  948. "name": project_name,
  949. "start": match.start(),
  950. "end": match.end(),
  951. "priority": priority
  952. })
  953. # 按位置排序并去重
  954. project_positions.sort(key=lambda x: x["start"])
  955. seen_positions = set()
  956. unique_projects = []
  957. for proj in project_positions:
  958. # 避免重复的项目(位置相近的同名项目)
  959. key = (proj["no"], proj["start"] // 100)
  960. if key not in seen_positions:
  961. seen_positions.add(key)
  962. unique_projects.append(proj)
  963. logger.info(f"[决算报告] 找到 {len(unique_projects)} 个单项工程")
  964. for proj in unique_projects:
  965. logger.debug(f"[决算报告] 项目 {proj['no']}: {proj['name']}")
  966. # 提取HTML表格及其位置
  967. table_pattern = r'<table[^>]*>(.*?)</table>'
  968. table_matches = list(re.finditer(table_pattern, markdown_content, re.DOTALL | re.IGNORECASE))
  969. logger.info(f"[决算报告] 找到 {len(table_matches)} 个HTML表格")
  970. # 解析每个表格
  971. for table_idx, table_match in enumerate(table_matches):
  972. table_html = table_match.group(1)
  973. table_pos = table_match.start()
  974. # 检查是否为单项工程投资完成情况表格
  975. if not _is_final_account_table(table_html, table_pos, section_start):
  976. logger.debug(f"[决算报告] 表格 {table_idx + 1} 不是单项工程投资完成情况表格,跳过")
  977. continue
  978. # 查找最近的项目
  979. matched_project = None
  980. for proj in unique_projects:
  981. if proj["end"] < table_pos:
  982. matched_project = proj
  983. if not matched_project:
  984. # 如果没有找到匹配的项目,使用表格索引作为项目序号
  985. logger.warning(f"[决算报告] 表格 {table_idx + 1} 未找到对应的项目名称")
  986. matched_project = {"no": table_idx + 1, "name": f"未知工程{table_idx + 1}"}
  987. logger.info(f"[决算报告] 解析表格 {table_idx + 1},关联项目: {matched_project['no']}-{matched_project['name']}")
  988. # 解析表格内容
  989. items = _parse_final_account_table_html(table_html, matched_project["no"], matched_project["name"])
  990. record.items.extend(items)
  991. logger.info(f"[决算报告] 解析完成,共 {len(record.items)} 条记录")
  992. logger.info("=" * 80)
  993. return record
  994. def _is_final_account_table(table_html: str, table_pos: int, section_start: int) -> bool:
  995. """
  996. 判断表格是否为单项工程投资完成情况表格
  997. 特征:
  998. 1. 位于"单项工程的投资完成情况"章节内
  999. 2. 包含"费用项目"、"概算金额"、"决算金额"、"超"、"节"等关键词
  1000. Args:
  1001. table_html: 表格HTML内容
  1002. table_pos: 表格在Markdown中的位置
  1003. section_start: 单项工程章节的起始位置
  1004. """
  1005. # 表格必须在单项工程章节内
  1006. if table_pos < section_start:
  1007. return False
  1008. table_text = table_html.lower()
  1009. # 必须包含的关键词
  1010. required_keywords = ["概算金额", "决算金额"]
  1011. # 至少包含一个的关键词
  1012. optional_keywords = ["费用项目", "建筑安装", "设备购置", "其他费用", "审定金额"]
  1013. has_required = all(kw.lower() in table_text for kw in required_keywords)
  1014. has_optional = any(kw.lower() in table_text for kw in optional_keywords)
  1015. return has_required and has_optional
  1016. def _parse_final_account_table_html(table_html: str, project_no: int, project_name: str) -> List[FinalAccountItem]:
  1017. """
  1018. 解析HTML表格内容
  1019. 表格结构:
  1020. 费用项目 | 概算金额 | 审定金额(不含税) | 增值税额 | 超节支金额 | 超节支率
  1021. Args:
  1022. table_html: HTML表格内容
  1023. project_no: 项目序号
  1024. project_name: 项目名称
  1025. Returns:
  1026. FinalAccountItem 列表
  1027. """
  1028. items = []
  1029. # 提取所有行
  1030. row_pattern = r'<tr[^>]*>(.*?)</tr>'
  1031. rows = re.findall(row_pattern, table_html, re.DOTALL | re.IGNORECASE)
  1032. if not rows:
  1033. return items
  1034. # 提取每行的单元格
  1035. cell_pattern = r'<td[^>]*>(.*?)</td>'
  1036. # 跳过表头行(通常前2-3行是表头)
  1037. data_start_idx = 0
  1038. for i, row in enumerate(rows):
  1039. cells = re.findall(cell_pattern, row, re.DOTALL | re.IGNORECASE)
  1040. row_text = " ".join(cells).lower()
  1041. # 检测数据开始行(包含"建筑安装"等费用项目名称)
  1042. if "建筑安装" in row_text or "设备购置" in row_text or "其他费用" in row_text:
  1043. data_start_idx = i
  1044. break
  1045. # 跳过表头行(包含"1"、"2"、"3"等列序号)
  1046. if re.match(r'^[\d\s=\-/]+$', row_text.replace(" ", "")):
  1047. continue
  1048. # 解析数据行
  1049. for row in rows[data_start_idx:]:
  1050. cells = re.findall(cell_pattern, row, re.DOTALL | re.IGNORECASE)
  1051. if len(cells) < 2:
  1052. continue
  1053. # 清理单元格内容
  1054. cells = [_clean_cell_text(cell) for cell in cells]
  1055. # 跳过空行
  1056. if not any(cells):
  1057. continue
  1058. # 获取费用项目名称(第一列)
  1059. fee_name = cells[0] if len(cells) > 0 else ""
  1060. # 跳过合计行
  1061. if any(kw in fee_name for kw in ["合计", "总计", "小计"]):
  1062. continue
  1063. # 只保留主要费用项目
  1064. valid_fee_names = ["建筑安装工程", "建筑安装", "设备购置", "其他费用"]
  1065. is_valid = any(kw in fee_name for kw in valid_fee_names)
  1066. if not is_valid:
  1067. continue
  1068. # 创建记录项
  1069. item = FinalAccountItem()
  1070. item.no = project_no
  1071. item.name = project_name
  1072. item.feeName = fee_name
  1073. # 解析数值列
  1074. # 根据列数确定索引
  1075. if len(cells) >= 6:
  1076. item.estimatedCost = _parse_number_str(cells[1])
  1077. item.approvedFinalAccountExcludingVat = _parse_number_str(cells[2])
  1078. item.vatAmount = _parse_number_str(cells[3])
  1079. item.costVariance = _parse_number_str(cells[4])
  1080. item.varianceRate = _parse_rate_str(cells[5])
  1081. elif len(cells) >= 5:
  1082. item.estimatedCost = _parse_number_str(cells[1])
  1083. item.approvedFinalAccountExcludingVat = _parse_number_str(cells[2])
  1084. item.vatAmount = _parse_number_str(cells[3])
  1085. item.costVariance = _parse_number_str(cells[4])
  1086. item.varianceRate = ""
  1087. items.append(item)
  1088. logger.debug(f"[决算报告] 解析记录: {project_name} - {fee_name} = {item.estimatedCost}")
  1089. return items
  1090. def _clean_cell_text(cell: str) -> str:
  1091. """清理单元格文本,移除HTML标签和多余空格"""
  1092. # 移除HTML标签
  1093. text = re.sub(r'<[^>]+>', '', cell)
  1094. # 移除多余空格
  1095. text = re.sub(r'\s+', ' ', text).strip()
  1096. return text
  1097. def _parse_number_str(value: str) -> str:
  1098. """解析数字字符串,保留原始精度"""
  1099. if not value or not value.strip():
  1100. return "0"
  1101. value = value.strip()
  1102. # 移除千分位逗号
  1103. value = value.replace(',', '')
  1104. # 移除非数字字符(保留负号和小数点)
  1105. cleaned = re.sub(r'[^\d.\-]', '', value)
  1106. if not cleaned or cleaned == '-':
  1107. return "0"
  1108. return cleaned
  1109. def _parse_rate_str(value: str) -> str:
  1110. """解析百分比字符串"""
  1111. if not value or not value.strip():
  1112. return "0%"
  1113. value = value.strip()
  1114. if '%' not in value:
  1115. # 提取数字部分并添加百分号
  1116. num_str = re.sub(r'[^\d.\-]', '', value)
  1117. if num_str and num_str != '-':
  1118. return f"{num_str}%"
  1119. return "0%"
  1120. return value