feat: ExcelParser 编排器(类型/性质/提取/汇总)

This commit is contained in:
lhl
2026-08-09 03:13:53 +08:00
parent a9cc3c9b90
commit 18da20dc0a
3 changed files with 80 additions and 1 deletions
+51
View File
@@ -0,0 +1,51 @@
from __future__ import annotations
from dataclasses import dataclass, field
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.merge_fill import forward_fill
from genesis.parsers.table_extractor import extract_table
from genesis.parsers.formatting_detector import collect_comments
from genesis.parsers.free_text_extractor import build_free_text_table, extract_text_blocks
@dataclass
class ExcelParseResult:
file_name: str
tables: list[ExcelTable] = field(default_factory=list)
comments: list[CellComment] = field(default_factory=list)
skipped: list[str] = field(default_factory=list)
class ExcelParser:
"""要件定义 Excel 解析入口。"""
def parse(self, path: str | Path) -> ExcelParseResult:
wb = open_workbook(path)
file_name = Path(path).name
result = ExcelParseResult(file_name=file_name)
for ws in wb.worksheets:
matrix = sheet_matrix(ws)
if not matrix:
result.skipped.append(ws.title)
continue
detected_type = detect_sheet_type(ws.title, matrix)
nature = classify_sheet(matrix)
if nature == SheetNature.FREE_TEXT:
blocks = extract_text_blocks(matrix)
result.tables.append(
build_free_text_table(ws.title, blocks, file_name, detected_type)
)
else:
merged = [
(r.min_row, r.min_col, r.max_row, r.max_col)
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))
result.comments.extend(collect_comments(ws, file_name))
return result