diff --git a/core/parser.py b/core/parser.py index bfc0319..c3caaae 100644 --- a/core/parser.py +++ b/core/parser.py @@ -115,14 +115,20 @@ class K3VoucherParser: # === 1. 确定数据区域 === y_headers = [] for text, conf, cx, cy, box in lines: - if text.strip() in ('借方', '贷方'): + if text.strip() in ('借方', '贷方', '科目', '摘要'): y_headers.append(cy) y_start = min(y_headers) if y_headers else 0 + # 数据区上界:列头上方500px(兼容科目代码在列头上方的布局) + y_data_start = max(0, y_start - 500) y_end = 999999 - for text, conf, cx, cy, box in lines: - if '合计' in text: - y_end = min(y_end, cy) - y_data_start = max(0, y_start - 150) + # 找数据区下方的"合计"行(排除顶部大写合计:带中文数字的) + import re as _re + heji_positions = [cy for text, conf, cx, cy, box in lines + if '合计' in text + and cy > y_data_start + and not _re.search(r'[壹贰叁肆伍陆柒捌玖拾佰仟万亿]', text)] + if heji_positions: + y_end = max(heji_positions) # 取最靠下的合计行 # === 2. 获取图像宽度 === all_xs = [] @@ -174,6 +180,16 @@ class K3VoucherParser: # === 4.5 摘要提取:从剩余 OCR 文字中找摘要 === code_y_min = min(c['y_pos'] for c in codes) + + # 找到表格头的y坐标("科目"或"摘要"列头) + table_header_y = min( + (cy for text, conf, cx, cy, box in self.lines + if text.strip() in ('科目', '摘要', '借方', '贷方')), + default=0 + ) + # 摘要搜索上界:向上1000px的宽范围(摘要有时在列头上方) + desc_upper_bound = max(0, code_y_min - 1000) + desc_candidates = [] search_lines = self.lines for text, conf, cx, cy, box in search_lines: @@ -202,7 +218,22 @@ class K3VoucherParser: continue if text.strip() in ('未到账', '已到账', '未达', '已达'): continue - if cy < code_y_min - 50: + # 过滤非摘要的日期/期号文本 + if re.match(r'\d{3}年第\d+期', text.strip()): + continue + if re.match(r'^[::]?\d{1,3}$', text.strip()): + continue + if text.strip().startswith('日期') or text.strip().startswith('业务日期'): + continue + if re.match(r'[\d,]+\s*\.\s*\d{2}', text.strip()): + continue + # 摘要列通常在凭证中部(x=400~950),排除右侧的公司名和左侧的金额 + if cx > 950 or cx < 400: + continue + # 过滤包含"公司"、"物业"、"有限"等公司相关词 + if re.search(r'(公司|有限|物业|集团|企业)', text): + continue + if cy < desc_upper_bound: continue # 去重:跳过 y 距离 < 30 的重复文本 if any(abs(d['y'] - cy) < 30 for d in desc_candidates): @@ -210,9 +241,31 @@ class K3VoucherParser: desc_candidates.append({'text': text, 'y': cy, 'x': cx}) desc_candidates.sort(key=lambda d: d['y']) - for i, desc in enumerate(desc_candidates): - if i < len(codes): - codes[i]['desc'] = desc['text'] + # 按y坐标最近邻匹配:对每个科目代码,找最近的摘要候选 + codes.sort(key=lambda c: c['y_pos']) + used_indices = set() + for code in codes: + best_dist = float('inf') + best_idx = -1 + for i, desc in enumerate(desc_candidates): + if i in used_indices: + continue + dist = abs(desc['y'] - code['y_pos']) + if dist < best_dist: + best_dist = dist + best_idx = i + if best_idx >= 0 and best_dist < 200: # 200px以内视为有效匹配 + code['desc'] = desc_candidates[best_idx]['text'] + used_indices.add(best_idx) + elif best_idx >= 0: + # 超过200px但只有这一个候选且是第一行,可能是共享摘要 + pass + + # 如果没有任何匹配到摘要,但候选列表有内容,用第一个候选作为共享摘要 + none_found = all(not c.get('desc') for c in codes) + if none_found and desc_candidates: + for code in codes: + code['desc'] = desc_candidates[0]['text'] # === 5. 提取金额 === amounts = []