55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from openpyxl import Workbook
|
|
|
|
from review_agent.models import ExportedSummaryFile, FileSummaryBatch
|
|
|
|
|
|
def _exports_dir(batch: FileSummaryBatch) -> Path:
|
|
root = Path(batch.work_dir or Path("media") / "file_summary" / batch.batch_no)
|
|
export_dir = root / "exports"
|
|
export_dir.mkdir(parents=True, exist_ok=True)
|
|
return export_dir
|
|
|
|
|
|
def generate_excel_export(batch: FileSummaryBatch) -> ExportedSummaryFile:
|
|
workbook = Workbook()
|
|
summary = workbook.active
|
|
summary.title = "汇总信息"
|
|
summary.append(["批次号", batch.batch_no])
|
|
summary.append(["产品名称", batch.product_name or "-"])
|
|
summary.append(["文件总数", batch.total_files])
|
|
summary.append(["统计成功", batch.success_files])
|
|
summary.append(["统计失败", batch.failed_files])
|
|
summary.append(["不支持", batch.unsupported_files])
|
|
summary.append(["不确定", batch.uncertain_files])
|
|
summary.append(["总页数", batch.total_pages])
|
|
|
|
detail = workbook.create_sheet("文件明细")
|
|
detail.append(["序号", "目录层级", "文件名", "类型", "页数", "路径", "状态", "重试次数", "异常说明"])
|
|
for item in batch.items.order_by("file_index"):
|
|
detail.append(
|
|
[
|
|
item.file_index,
|
|
item.directory_level,
|
|
item.file_name,
|
|
item.file_type,
|
|
item.page_count,
|
|
item.relative_path,
|
|
item.statistics_status,
|
|
item.retry_count,
|
|
item.error_message,
|
|
]
|
|
)
|
|
|
|
path = _exports_dir(batch) / f"{batch.batch_no}-summary.xlsx"
|
|
workbook.save(path)
|
|
return ExportedSummaryFile.objects.create(
|
|
batch=batch,
|
|
export_type=ExportedSummaryFile.ExportType.EXCEL,
|
|
file_name=path.name,
|
|
storage_path=str(path),
|
|
)
|