82 lines
3.6 KiB
Python
82 lines
3.6 KiB
Python
"""按市场成果生成 Result.xlsx。"""
|
|
from contextlib import closing
|
|
from pathlib import Path
|
|
import sqlite3
|
|
|
|
from . import config
|
|
|
|
BUILDING_COLUMNS = [
|
|
("building_id", "楼宇ID", False),
|
|
("longitude", "经度", False),
|
|
("latitude", "纬度", False),
|
|
("mro_ratio_yd", "移动采样点占比", True),
|
|
("mro_ratio_dx", "电信采样点占比", True),
|
|
("mro_ratio_lt", "联通采样点占比", True),
|
|
("mro_ratio_gd", "广电采样点占比", True),
|
|
("mro_ratio_dxlt", "电联采样点占比", True),
|
|
]
|
|
AOI_COLUMNS = [
|
|
("所属市场网格", "所属市场网格", False),
|
|
("AOI唯一标签", "AOI唯一标签", False),
|
|
("AOI名称", "AOI名称", False),
|
|
("移动C端采样点占比", "移动C端采样点占比", True),
|
|
("联通C端采样点占比", "联通C端采样点占比", True),
|
|
("电信C端采样点占比", "电信C端采样点占比", True),
|
|
("广电C端采样点占比", "广电C端采样点占比", True),
|
|
("电联C端采样点占比", "电联C端采样点占比", True),
|
|
]
|
|
|
|
|
|
def _percent_text(value):
|
|
"""源数据已是百分数,写成文本 99.65%,不依赖单元格格式。"""
|
|
if value is None or value == "":
|
|
return None
|
|
text = str(value).strip()
|
|
if text.endswith("%"):
|
|
return text
|
|
return f"{float(text):.2f}%"
|
|
|
|
|
|
def _write_sheet(workbook, title, gpkg_path, table_name, columns, 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([label for _, label, _ in columns])
|
|
quoted = ", ".join('"' + name.replace('"', '""') + '"' for name, _, _ in columns)
|
|
with closing(sqlite3.connect(gpkg_path)) as conn:
|
|
rows = list(conn.execute(f'SELECT {quoted} FROM "{table_name}"'))
|
|
percent_at = [index for index, (_, _, percent) in enumerate(columns) if percent]
|
|
for row in rows:
|
|
values = list(row)
|
|
for index in percent_at:
|
|
values[index] = _percent_text(values[index])
|
|
sheet.append(values)
|
|
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(columns))}{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_count = _write_sheet(workbook, "移动C端采样点比例_楼宇", building_gpkg, "NRBuildingsData", BUILDING_COLUMNS, [16, 12, 12, 16, 16, 16, 16, 16])
|
|
aoi_count = _write_sheet(workbook, "移动C端采样点比例_AOI", aoi_gpkg, config.AOI_LAYER, AOI_COLUMNS, [16, 18, 40, 20, 20, 20, 20, 20])
|
|
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}
|