refine result preview layout
This commit is contained in:
@@ -48,6 +48,7 @@ def run_processing(
|
||||
result_name: str,
|
||||
show_not_match: bool,
|
||||
show_all_infos: bool,
|
||||
preview_rows: int = 20,
|
||||
) -> ProcessingResult:
|
||||
if mode not in {"auto", "v1", "v2"}:
|
||||
raise ProcessingError("处理模式不正确。")
|
||||
@@ -123,6 +124,9 @@ def run_processing(
|
||||
if not xlsx_files:
|
||||
raise ProcessingError("处理完成但没有生成 Excel 文件,请检查数据结构。")
|
||||
|
||||
for xlsx_file in xlsx_files:
|
||||
_remove_default_empty_sheet(xlsx_file)
|
||||
|
||||
result_zip = job_dir / "result.zip"
|
||||
_create_result_zip(output_dir, result_zip)
|
||||
return ProcessingResult(
|
||||
@@ -130,7 +134,7 @@ def run_processing(
|
||||
mode=selected_mode,
|
||||
output_dir=output_dir,
|
||||
zip_path=result_zip,
|
||||
files=[_summarize_workbook(path, output_dir) for path in xlsx_files],
|
||||
files=[_summarize_workbook(path, output_dir, preview_rows) for path in xlsx_files],
|
||||
)
|
||||
|
||||
|
||||
@@ -143,6 +147,24 @@ def create_result_zip(job_dir: Path) -> Path:
|
||||
return result_zip
|
||||
|
||||
|
||||
def summarize_job(job_dir: Path, preview_rows: int = 20) -> ProcessingResult:
|
||||
output_dir = job_dir / "output"
|
||||
if not output_dir.exists():
|
||||
raise ProcessingError("结果目录不存在。")
|
||||
xlsx_files = sorted(output_dir.rglob("*.xlsx"))
|
||||
if not xlsx_files:
|
||||
raise ProcessingError("结果文件不存在。")
|
||||
result_zip = job_dir / "result.zip"
|
||||
mode = _read_mode(job_dir)
|
||||
return ProcessingResult(
|
||||
job_id=job_dir.name,
|
||||
mode=mode,
|
||||
output_dir=output_dir,
|
||||
zip_path=result_zip,
|
||||
files=[_summarize_workbook(path, output_dir, preview_rows) for path in xlsx_files],
|
||||
)
|
||||
|
||||
|
||||
def find_output_file(job_dir: Path, relpath: str) -> Path:
|
||||
output_dir = (job_dir / "output").resolve()
|
||||
target = (output_dir / relpath).resolve()
|
||||
@@ -153,6 +175,16 @@ def find_output_file(job_dir: Path, relpath: str) -> Path:
|
||||
return target
|
||||
|
||||
|
||||
def _read_mode(job_dir: Path) -> str:
|
||||
log_path = job_dir / "process.log"
|
||||
if not log_path.exists():
|
||||
return "unknown"
|
||||
first_line = log_path.read_text(encoding="utf-8", errors="replace").splitlines()[0:1]
|
||||
if first_line and first_line[0].startswith("mode="):
|
||||
return first_line[0].split("=", 1)[1]
|
||||
return "unknown"
|
||||
|
||||
|
||||
def _safe_extract(zip_path: Path, target_dir: Path) -> None:
|
||||
try:
|
||||
with zipfile.ZipFile(zip_path) as zf:
|
||||
@@ -223,13 +255,33 @@ def _create_result_zip(output_dir: Path, result_zip: Path) -> None:
|
||||
zf.write(path, path.relative_to(output_dir))
|
||||
|
||||
|
||||
def _summarize_workbook(path: Path, output_dir: Path) -> ExcelSummary:
|
||||
def _remove_default_empty_sheet(path: Path) -> None:
|
||||
workbook = load_workbook(path)
|
||||
try:
|
||||
if "Sheet" in workbook.sheetnames and len(workbook.sheetnames) > 1:
|
||||
sheet = workbook["Sheet"]
|
||||
if _is_empty_sheet(sheet):
|
||||
workbook.remove(sheet)
|
||||
workbook.save(path)
|
||||
finally:
|
||||
workbook.close()
|
||||
|
||||
|
||||
def _is_empty_sheet(sheet) -> bool:
|
||||
for row in sheet.iter_rows(values_only=True):
|
||||
for value in row:
|
||||
if value not in (None, ""):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _summarize_workbook(path: Path, output_dir: Path, preview_rows: int) -> ExcelSummary:
|
||||
sheets: list[SheetSummary] = []
|
||||
workbook = load_workbook(path, read_only=True, data_only=True)
|
||||
try:
|
||||
for sheet in workbook.worksheets:
|
||||
preview: list[list[str]] = []
|
||||
for row in sheet.iter_rows(max_row=6, values_only=True):
|
||||
for row in sheet.iter_rows(max_row=max(2, min(preview_rows, 200)), values_only=True):
|
||||
preview.append([_cell_to_text(value) for value in row])
|
||||
sheets.append(
|
||||
SheetSummary(
|
||||
|
||||
Reference in New Issue
Block a user