fix: 里程碑2 最终评审 must-fix(header_row 接线 + formatting 回填)

This commit is contained in:
lhl
2026-08-09 03:25:32 +08:00
parent 3307d7ba72
commit eebed38886
4 changed files with 59 additions and 5 deletions
+17 -3
View File
@@ -6,10 +6,10 @@ from pathlib import Path
from genesis.data_models import CellComment, ExcelTable
from genesis.parsers.excel_reader import open_workbook, sheet_matrix
from genesis.parsers.sheet_detector import detect_sheet_type
from genesis.parsers.sheet_nature import SheetNature, classify_sheet
from genesis.parsers.sheet_nature import SheetNature, classify_sheet, find_header_row
from genesis.parsers.merge_fill import forward_fill
from genesis.parsers.table_extractor import extract_table
from genesis.parsers.formatting_detector import collect_comments
from genesis.parsers.formatting_detector import cell_formatting, collect_comments
from genesis.parsers.free_text_extractor import build_free_text_table, extract_text_blocks
@@ -46,6 +46,20 @@ class ExcelParser:
for r in ws.merged_cells.ranges
]
filled = forward_fill(matrix, merged) if merged else matrix
result.tables.append(extract_table(ws.title, filled, file_name, detected_type))
header_row = find_header_row(filled)
if header_row < 0:
header_row = 0
fmt_map = {}
for row in ws.iter_rows():
for cell in row:
fmt = cell_formatting(cell)
if fmt is not None:
fmt_map[(cell.row - 1, cell.column - 1)] = fmt
result.tables.append(
extract_table(
ws.title, filled, file_name, detected_type,
header_row=header_row, formatting_map=fmt_map,
)
)
result.comments.extend(collect_comments(ws, file_name))
return result
+8 -2
View File
@@ -2,7 +2,7 @@ from __future__ import annotations
from typing import Any
from genesis.data_models import CellValue, ExcelTable, Provenance, SheetType
from genesis.data_models import CellFormatting, CellValue, ExcelTable, Provenance, SheetType
def column_letter(index: int) -> str:
@@ -20,13 +20,18 @@ def extract_table(
file_name: str,
detected_type: SheetType,
header_row: int = 0,
formatting_map: dict[tuple[int, int], CellFormatting] | None = None,
) -> ExcelTable:
"""从矩阵提取表格:首行视为表头,其后为数据行。"""
"""从矩阵提取表格:首行视为表头,其后为数据行。
formatting_map:矩阵坐标 (row, col)0 起)→ CellFormatting,用于还原取消线/背景色等样式。
"""
if not matrix:
return ExcelTable(
name=sheet_name, detected_type=detected_type,
extraction_method="openpyxl", headers=[], rows=[],
)
fmt_map = formatting_map or {}
headers = [str(c) if c is not None else "" for c in matrix[header_row]]
rows = []
for r in range(header_row + 1, len(matrix)):
@@ -42,6 +47,7 @@ def extract_table(
column=column_letter(c + 1),
column_header=h,
),
formatting=fmt_map.get((r, c)),
)
rows.append(row_dict)
return ExcelTable(