feat: 市场成果包增加 Result Excel 汇总表

This commit is contained in:
nixevol
2026-09-23 10:09:11 +08:00
parent e8ceef8f95
commit 697e09fbb7
3 changed files with 55 additions and 1 deletions
+46
View File
@@ -0,0 +1,46 @@
"""按市场成果生成 Result.xlsx。"""
from pathlib import Path
import sqlite3
from . import config
def _write_sheet(workbook, title, gpkg_path, table_name, fields, widths):
from openpyxl.styles import Font, PatternFill, Border, Side, Alignment
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.table import Table, TableStyleInfo
sheet = workbook.create_sheet(title[:31])
sheet.append(fields)
quoted = ', '.join('"' + field.replace('"', '""') + '"' for field in fields)
with sqlite3.connect(gpkg_path) as conn:
for row in conn.execute(f'SELECT {quoted} FROM "{table_name}"'):
sheet.append(row)
header_fill = PatternFill("solid", fgColor="D9EAF7")
thin = Side(style="thin", color="D9E2F3")
for cell in sheet[1]:
cell.font = Font(name="等线", size=11)
cell.fill = header_fill
cell.border = Border(bottom=thin)
cell.alignment = Alignment(vertical="center")
for index, width in enumerate(widths, 1):
sheet.column_dimensions[get_column_letter(index)].width = width
sheet.auto_filter.ref = f"A1:{get_column_letter(len(fields))}{sheet.max_row}"
sheet.freeze_panes = "A2"
table = Table(displayName=f"Table{len(workbook.worksheets)}", ref=sheet.auto_filter.ref)
table.tableStyleInfo = TableStyleInfo(name="TableStyleMedium2", showFirstColumn=False, showLastColumn=False, showRowStripes=False, showColumnStripes=False)
sheet.add_table(table)
return sheet.max_row - 1
def create_result_xlsx(output_path, building_gpkg, aoi_gpkg):
from openpyxl import Workbook
workbook = Workbook()
workbook.remove(workbook.active)
building_fields = ["building_id", "longitude", "latitude", "mro_ratio_yd", "mro_ratio_dx", "mro_ratio_lt", "mro_ratio_gd", "mro_ratio_dxlt"]
aoi_fields = ["所属市场网格", "AOI唯一标签", "AOI名称", "移动C端采样点占比", "联通C端采样点占比", "电信C端采样点占比", "广电C端采样点占比", "电联C端采样点占比"]
building_count = _write_sheet(workbook, "移动C端采样点比例_楼宇", building_gpkg, "NRBuildingsData", building_fields, [15, 12, 12, 14, 14, 14, 14, 14])
aoi_count = _write_sheet(workbook, "移动C端采样点比例_AOI", aoi_gpkg, config.AOI_LAYER, aoi_fields, [16, 18, 40, 18, 18, 18, 18, 18])
output_path = Path(output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
workbook.save(output_path)
return {"output": str(output_path), "building_rows": building_count, "aoi_rows": aoi_count}