#!/usr/bin/env python
|
# -*- coding: utf-8 -*-
|
"""Build a formal inspection report DOCX from the verified result workbook."""
|
|
from __future__ import annotations
|
|
import argparse
|
import json
|
from collections import Counter
|
from pathlib import Path
|
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple
|
|
from docx import Document
|
from docx.enum.section import WD_ORIENT, WD_SECTION
|
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT, WD_TABLE_ALIGNMENT
|
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
from docx.oxml import OxmlElement
|
from docx.oxml.ns import qn
|
from docx.shared import Cm, Pt, RGBColor
|
from openpyxl import load_workbook
|
|
TITLE_FONT = "黑体"
|
BODY_FONT = "仿宋_GB2312"
|
BLACK = RGBColor(0, 0, 0)
|
BORDER_COLOR = "D9D9D9"
|
|
ALIGN_LEFT = "left"
|
ALIGN_CENTER = "center"
|
ALIGN_RIGHT = "right"
|
|
KAIXIANGDA_MONTHLY = (
|
("1月", 12726500, 8517662.20, 4208837.80, 0.4941),
|
("2月", 19031731, 14615765.90, 4415965.10, 0.3021),
|
("3月", 24968861, 22850478.60, 2118382.40, 0.0927),
|
("4月", 18847414, 12541603.50, 6305810.50, 0.5028),
|
("5月", 19323113, 13188737.70, 6134375.30, 0.4651),
|
("6月", 18645521, 12284753.20, 6360767.80, 0.5178),
|
("7月", 18092503, 12127504.44, 5964998.56, 0.4919),
|
("8月", 17992191, 12499398.54, 5492792.46, 0.4394),
|
("9月", 18024120, 11990746.03, 6033373.97, 0.5032),
|
)
|
KAIXIANGDA_TOTAL_REPORT = 167651954.00
|
KAIXIANGDA_TOTAL_CORRECT = 120616650.11
|
KAIXIANGDA_TOTAL_OVER = 47035303.89
|
KAIXIANGDA_TOTAL_RATE = 0.3899569700
|
|
|
def set_run_font(run: Any, name: str, size: float, bold: Optional[bool] = None) -> None:
|
run.font.name = name
|
run.font.size = Pt(size)
|
run.font.color.rgb = BLACK
|
if bold is not None:
|
run.bold = bold
|
rpr = run._element.get_or_add_rPr()
|
rfonts = rpr.get_or_add_rFonts()
|
rfonts.set(qn("w:ascii"), name)
|
rfonts.set(qn("w:hAnsi"), name)
|
rfonts.set(qn("w:eastAsia"), name)
|
rfonts.set(qn("w:cs"), name)
|
|
|
def remove_paragraph_borders(paragraph: Any) -> None:
|
ppr = paragraph._p.get_or_add_pPr()
|
for node in list(ppr.findall(qn("w:pBdr"))):
|
ppr.remove(node)
|
|
|
def remove_style_borders(style: Any) -> None:
|
ppr = style.element.find(qn("w:pPr"))
|
if ppr is None:
|
return
|
for node in list(ppr.findall(qn("w:pBdr"))):
|
ppr.remove(node)
|
|
|
def configure_section(section: Any, landscape: bool) -> None:
|
if landscape:
|
section.orientation = WD_ORIENT.LANDSCAPE
|
section.page_width = Cm(29.7)
|
section.page_height = Cm(21.0)
|
section.top_margin = Cm(2.54)
|
section.bottom_margin = Cm(2.54)
|
section.left_margin = Cm(2.0)
|
section.right_margin = Cm(2.0)
|
section.header_distance = Cm(1.501)
|
section.footer_distance = Cm(1.75)
|
else:
|
section.orientation = WD_ORIENT.PORTRAIT
|
section.page_width = Cm(21.0)
|
section.page_height = Cm(29.7)
|
section.top_margin = Cm(2.54)
|
section.bottom_margin = Cm(2.54)
|
section.left_margin = Cm(3.175)
|
section.right_margin = Cm(3.175)
|
section.header_distance = Cm(1.501)
|
section.footer_distance = Cm(1.75)
|
|
|
def add_section(doc: Document, landscape: bool) -> None:
|
section = doc.add_section(WD_SECTION.NEW_PAGE)
|
configure_section(section, landscape=landscape)
|
section.footer.is_linked_to_previous = True
|
|
|
def configure_document(doc: Document) -> None:
|
section = doc.sections[0]
|
configure_section(section, landscape=False)
|
|
normal = doc.styles["Normal"]
|
normal.font.name = BODY_FONT
|
normal.font.size = Pt(16)
|
normal.font.color.rgb = BLACK
|
normal.paragraph_format.line_spacing = Pt(30)
|
normal.paragraph_format.space_before = Pt(0)
|
normal.paragraph_format.space_after = Pt(0)
|
normal.paragraph_format.first_line_indent = Pt(32)
|
normal.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
|
normal.paragraph_format.widow_control = True
|
rpr = normal.element.get_or_add_rPr()
|
rfonts = rpr.get_or_add_rFonts()
|
for key in ("w:ascii", "w:hAnsi", "w:eastAsia", "w:cs"):
|
rfonts.set(qn(key), BODY_FONT)
|
|
title = doc.styles["Title"]
|
title.font.name = TITLE_FONT
|
title.font.size = Pt(22)
|
title.font.bold = False
|
title.font.color.rgb = BLACK
|
title.font.underline = False
|
title.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
title.paragraph_format.space_before = Pt(0)
|
title.paragraph_format.space_after = Pt(12)
|
title.paragraph_format.line_spacing = Pt(34)
|
title.paragraph_format.first_line_indent = Pt(0)
|
remove_style_borders(title)
|
trpr = title.element.get_or_add_rPr()
|
trfonts = trpr.get_or_add_rFonts()
|
for key in ("w:ascii", "w:hAnsi", "w:eastAsia", "w:cs"):
|
trfonts.set(qn(key), TITLE_FONT)
|
|
heading1 = doc.styles["Heading 1"]
|
heading1.font.name = TITLE_FONT
|
heading1.font.size = Pt(16)
|
heading1.font.bold = False
|
heading1.font.color.rgb = BLACK
|
heading1.paragraph_format.first_line_indent = Pt(32)
|
heading1.paragraph_format.left_indent = Pt(0)
|
heading1.paragraph_format.space_before = Pt(0)
|
heading1.paragraph_format.space_after = Pt(0)
|
heading1.paragraph_format.line_spacing = Pt(30)
|
heading1.paragraph_format.keep_with_next = True
|
h1rpr = heading1.element.get_or_add_rPr()
|
h1fonts = h1rpr.get_or_add_rFonts()
|
for key in ("w:ascii", "w:hAnsi", "w:eastAsia", "w:cs"):
|
h1fonts.set(qn(key), TITLE_FONT)
|
|
heading2 = doc.styles["Heading 2"]
|
heading2.font.name = BODY_FONT
|
heading2.font.size = Pt(16)
|
heading2.font.bold = True
|
heading2.font.color.rgb = BLACK
|
heading2.paragraph_format.first_line_indent = Pt(32)
|
heading2.paragraph_format.left_indent = Pt(0)
|
heading2.paragraph_format.space_before = Pt(0)
|
heading2.paragraph_format.space_after = Pt(0)
|
heading2.paragraph_format.line_spacing = Pt(30)
|
heading2.paragraph_format.keep_with_next = True
|
h2rpr = heading2.element.get_or_add_rPr()
|
h2fonts = h2rpr.get_or_add_rFonts()
|
for key in ("w:ascii", "w:hAnsi", "w:eastAsia", "w:cs"):
|
h2fonts.set(qn(key), BODY_FONT)
|
|
for section in doc.sections:
|
footer = section.footer
|
p = footer.paragraphs[0]
|
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
p.paragraph_format.first_line_indent = Pt(0)
|
p.paragraph_format.space_before = Pt(0)
|
p.paragraph_format.space_after = Pt(0)
|
prefix = p.add_run("— ")
|
set_run_font(prefix, BODY_FONT, 12)
|
add_page_field(p)
|
suffix = p.add_run(" —")
|
set_run_font(suffix, BODY_FONT, 12)
|
|
|
def add_page_field(paragraph: Any) -> None:
|
run = paragraph.add_run()
|
set_run_font(run, BODY_FONT, 12)
|
begin = OxmlElement("w:fldChar")
|
begin.set(qn("w:fldCharType"), "begin")
|
instr = OxmlElement("w:instrText")
|
instr.set(qn("xml:space"), "preserve")
|
instr.text = " PAGE "
|
separate = OxmlElement("w:fldChar")
|
separate.set(qn("w:fldCharType"), "separate")
|
text = OxmlElement("w:t")
|
text.text = "1"
|
end = OxmlElement("w:fldChar")
|
end.set(qn("w:fldCharType"), "end")
|
run._r.extend([begin, instr, separate, text, end])
|
|
|
def add_title(doc: Document, text: str) -> None:
|
paragraph = doc.add_paragraph(style="Title")
|
remove_paragraph_borders(paragraph)
|
run = paragraph.add_run(text)
|
set_run_font(run, TITLE_FONT, 22, False)
|
run.font.underline = False
|
run.font.color.rgb = BLACK
|
|
|
def add_heading(doc: Document, text: str, level: int = 1) -> None:
|
paragraph = doc.add_paragraph(style="Heading %d" % level)
|
run = paragraph.add_run(text)
|
set_run_font(run, TITLE_FONT if level == 1 else BODY_FONT, 16, level != 1)
|
|
|
def add_body(doc: Document, text: str, bold_lead: str = "") -> None:
|
paragraph = doc.add_paragraph()
|
paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
|
paragraph.paragraph_format.first_line_indent = Pt(32)
|
paragraph.paragraph_format.line_spacing = Pt(30)
|
if bold_lead:
|
lead = paragraph.add_run(bold_lead)
|
set_run_font(lead, BODY_FONT, 16, True)
|
run = paragraph.add_run(text)
|
set_run_font(run, BODY_FONT, 16, False)
|
|
|
def add_caption(doc: Document, text: str) -> None:
|
paragraph = doc.add_paragraph()
|
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
paragraph.paragraph_format.first_line_indent = Pt(0)
|
paragraph.paragraph_format.space_before = Pt(6)
|
paragraph.paragraph_format.space_after = Pt(3)
|
paragraph.paragraph_format.line_spacing = Pt(18)
|
paragraph.paragraph_format.keep_with_next = True
|
run = paragraph.add_run(text)
|
set_run_font(run, BODY_FONT, 12, True)
|
|
|
def clear_cell_shading(cell: Any) -> None:
|
tcpr = cell._tc.get_or_add_tcPr()
|
for shd in list(tcpr.findall(qn("w:shd"))):
|
tcpr.remove(shd)
|
|
|
def set_cell_border(cell: Any) -> None:
|
tcpr = cell._tc.get_or_add_tcPr()
|
borders = tcpr.first_child_found_in("w:tcBorders")
|
if borders is None:
|
borders = OxmlElement("w:tcBorders")
|
tcpr.append(borders)
|
for edge in ("top", "left", "bottom", "right", "insideH", "insideV"):
|
tag = "w:" + edge
|
element = borders.find(qn(tag))
|
if element is None:
|
element = OxmlElement(tag)
|
borders.append(element)
|
element.set(qn("w:val"), "single")
|
element.set(qn("w:sz"), "4")
|
element.set(qn("w:space"), "0")
|
element.set(qn("w:color"), BORDER_COLOR)
|
|
|
def set_table_borders_gray(table: Any) -> None:
|
tbl_pr = table._tbl.tblPr
|
tbl_borders = tbl_pr.first_child_found_in("w:tblBorders")
|
if tbl_borders is None:
|
tbl_borders = OxmlElement("w:tblBorders")
|
tbl_pr.append(tbl_borders)
|
for edge in ("top", "left", "bottom", "right", "insideH", "insideV"):
|
tag = "w:" + edge
|
element = tbl_borders.find(qn(tag))
|
if element is None:
|
element = OxmlElement(tag)
|
tbl_borders.append(element)
|
element.set(qn("w:val"), "single")
|
element.set(qn("w:sz"), "4")
|
element.set(qn("w:space"), "0")
|
element.set(qn("w:color"), BORDER_COLOR)
|
|
|
def set_cell_margins(cell: Any, top: int = 90, start: int = 100, bottom: int = 90, end: int = 100) -> None:
|
tc = cell._tc
|
tcpr = tc.get_or_add_tcPr()
|
tcmar = tcpr.first_child_found_in("w:tcMar")
|
if tcmar is None:
|
tcmar = OxmlElement("w:tcMar")
|
tcpr.append(tcmar)
|
for margin, value in (("top", top), ("start", start), ("bottom", bottom), ("end", end)):
|
node = tcmar.find(qn("w:" + margin))
|
if node is None:
|
node = OxmlElement("w:" + margin)
|
tcmar.append(node)
|
node.set(qn("w:w"), str(value))
|
node.set(qn("w:type"), "dxa")
|
|
|
def set_repeat_table_header(row: Any) -> None:
|
trpr = row._tr.get_or_add_trPr()
|
tbl_header = OxmlElement("w:tblHeader")
|
tbl_header.set(qn("w:val"), "true")
|
trpr.append(tbl_header)
|
|
|
def prevent_row_split(row: Any) -> None:
|
trpr = row._tr.get_or_add_trPr()
|
cant_split = OxmlElement("w:cantSplit")
|
trpr.append(cant_split)
|
|
|
def set_table_widths(table: Any, widths_cm: Sequence[float]) -> None:
|
table.autofit = False
|
dxa_widths = [int(width * 567) for width in widths_cm]
|
tbl_pr = table._tbl.tblPr
|
|
tbl_layout = tbl_pr.first_child_found_in("w:tblLayout")
|
if tbl_layout is None:
|
tbl_layout = OxmlElement("w:tblLayout")
|
tbl_pr.append(tbl_layout)
|
tbl_layout.set(qn("w:type"), "fixed")
|
|
tbl_w = tbl_pr.first_child_found_in("w:tblW")
|
if tbl_w is None:
|
tbl_w = OxmlElement("w:tblW")
|
tbl_pr.append(tbl_w)
|
tbl_w.set(qn("w:type"), "dxa")
|
tbl_w.set(qn("w:w"), str(sum(dxa_widths)))
|
|
grid_cols = table._tbl.tblGrid.findall(qn("w:gridCol"))
|
if len(grid_cols) != len(widths_cm):
|
raise ValueError("table grid column count mismatch")
|
for grid_col, width in zip(grid_cols, dxa_widths):
|
grid_col.set(qn("w:w"), str(width))
|
|
for row in table.rows:
|
for index, width in enumerate(widths_cm):
|
row.cells[index].width = Cm(width)
|
|
|
def set_cell_text(cell: Any, text: str, font_size: float, bold: bool, alignment: str) -> None:
|
cell.text = ""
|
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
|
clear_cell_shading(cell)
|
set_cell_border(cell)
|
set_cell_margins(cell)
|
paragraph = cell.paragraphs[0]
|
paragraph.paragraph_format.first_line_indent = Pt(0)
|
paragraph.paragraph_format.space_before = Pt(0)
|
paragraph.paragraph_format.space_after = Pt(0)
|
paragraph.paragraph_format.line_spacing = Pt(14 if font_size <= 10.5 else 16)
|
paragraph.alignment = {
|
ALIGN_LEFT: WD_ALIGN_PARAGRAPH.LEFT,
|
ALIGN_CENTER: WD_ALIGN_PARAGRAPH.CENTER,
|
ALIGN_RIGHT: WD_ALIGN_PARAGRAPH.RIGHT,
|
}[alignment]
|
run = paragraph.add_run(str(text))
|
set_run_font(run, BODY_FONT, font_size, bold)
|
|
|
def add_table(
|
doc: Document,
|
headers: Sequence[str],
|
rows: Sequence[Sequence[str]],
|
widths_cm: Sequence[float],
|
alignments: Sequence[str],
|
header_size: float = 12,
|
body_size: float = 10.5,
|
) -> Any:
|
table = doc.add_table(rows=1, cols=len(headers))
|
table.alignment = WD_TABLE_ALIGNMENT.CENTER
|
table.style = "Table Grid"
|
table.autofit = False
|
set_table_borders_gray(table)
|
set_table_widths(table, widths_cm)
|
header = table.rows[0]
|
set_repeat_table_header(header)
|
prevent_row_split(header)
|
for index, text in enumerate(headers):
|
cell = header.cells[index]
|
set_cell_text(cell, text, header_size, True, ALIGN_CENTER)
|
for values in rows:
|
row = table.add_row()
|
prevent_row_split(row)
|
for index, value in enumerate(values):
|
cell = row.cells[index]
|
set_cell_text(cell, value, body_size, False, alignments[index])
|
set_table_widths(table, widths_cm)
|
spacer = doc.add_paragraph()
|
spacer.paragraph_format.first_line_indent = Pt(0)
|
spacer.paragraph_format.space_before = Pt(0)
|
spacer.paragraph_format.space_after = Pt(0)
|
spacer.paragraph_format.line_spacing = Pt(6)
|
return table
|
|
|
def compact_table(table: Any) -> None:
|
for row in table.rows:
|
for cell in row.cells:
|
set_cell_margins(cell, top=40, start=60, bottom=40, end=60)
|
for paragraph in cell.paragraphs:
|
paragraph.paragraph_format.space_before = Pt(0)
|
paragraph.paragraph_format.space_after = Pt(0)
|
paragraph.paragraph_format.line_spacing = Pt(12)
|
|
|
CITY_ORDER = {
|
"\u6b66\u6c49\u5e02": 0,
|
"\u9ec4\u77f3\u5e02": 1,
|
"\u5341\u5830\u5e02": 2,
|
"\u5b9c\u660c\u5e02": 3,
|
"\u8944\u9633\u5e02": 4,
|
"\u9102\u5dde\u5e02": 5,
|
"\u8346\u95e8\u5e02": 6,
|
"\u5b5d\u611f\u5e02": 7,
|
"\u8346\u5dde\u5e02": 8,
|
"\u9ec4\u5188\u5e02": 9,
|
"\u54b8\u5b81\u5e02": 10,
|
"\u968f\u5dde\u5e02": 11,
|
"\u6069\u65bd\u5dde": 12,
|
"\u4ed9\u6843\u5e02": 13,
|
"\u6f5c\u6c5f\u5e02": 14,
|
"\u5929\u95e8\u5e02": 15,
|
"\u6797\u533a": 16,
|
}
|
CITY_FULL_NAMES = {
|
"\u6b66\u6c49": "\u6b66\u6c49\u5e02",
|
"\u9ec4\u77f3": "\u9ec4\u77f3\u5e02",
|
"\u5341\u5830": "\u5341\u5830\u5e02",
|
"\u5b9c\u660c": "\u5b9c\u660c\u5e02",
|
"\u8944\u9633": "\u8944\u9633\u5e02",
|
"\u9102\u5dde": "\u9102\u5dde\u5e02",
|
"\u8346\u95e8": "\u8346\u95e8\u5e02",
|
"\u5b5d\u611f": "\u5b5d\u611f\u5e02",
|
"\u8346\u5dde": "\u8346\u5dde\u5e02",
|
"\u9ec4\u5188": "\u9ec4\u5188\u5e02",
|
"\u54b8\u5b81": "\u54b8\u5b81\u5e02",
|
"\u968f\u5dde": "\u968f\u5dde\u5e02",
|
"\u6069\u65bd\u5dde": "\u6069\u65bd\u5dde",
|
"\u4ed9\u6843": "\u4ed9\u6843\u5e02",
|
"\u6f5c\u6c5f": "\u6f5c\u6c5f\u5e02",
|
"\u5929\u95e8": "\u5929\u95e8\u5e02",
|
"\u6797\u533a": "\u6797\u533a",
|
}
|
|
|
def full_city_name(value: Any) -> str:
|
text = str(value or "").strip()
|
return CITY_FULL_NAMES.get(text, text)
|
|
|
def short_city_name(value: Any) -> str:
|
text = full_city_name(value)
|
return text[:-1] if text.endswith("\u5e02") else text
|
|
|
def city_sort_key(value: Any) -> Tuple[int, str]:
|
city = full_city_name(value)
|
return CITY_ORDER.get(city, 99), city
|
|
|
def fmt_int(value: Any) -> str:
|
return f"{int(round(float(value or 0))):,}"
|
|
|
def fmt_num(value: Any, digits: int = 2) -> str:
|
return f"{float(value or 0):,.{digits}f}"
|
|
|
def fmt_wan_ton_km(value: Any, digits: int = 2) -> str:
|
return fmt_num(float(value or 0) / 10000, digits)
|
|
|
def fmt_pct(value: Any, signed: bool = True) -> str:
|
rate = float(value or 0) * 100
|
return (f"{rate:+.2f}%" if signed else f"{rate:.2f}%")
|
|
|
def read_enterprises(path: Path) -> List[Dict[str, Any]]:
|
wb = load_workbook(path, data_only=True)
|
enterprises: List[Dict[str, Any]] = []
|
if "\u603b\u4f53\u60c5\u51b5" in wb.sheetnames:
|
sheet = wb["\u603b\u4f53\u60c5\u51b5"]
|
for row in range(4, sheet.max_row + 1):
|
if not sheet.cell(row, 2).value:
|
continue
|
enterprises.append({
|
"city": sheet.cell(row, 1).value or "",
|
"enterprise": sheet.cell(row, 2).value or "",
|
"ledger_vehicles": sheet.cell(row, 7).value or 0,
|
"month_turnover": sheet.cell(row, 6).value or 0,
|
"ledger_turnover": sheet.cell(row, 9).value or 0,
|
"report_vs_ledger": sheet.cell(row, 11).value or 0,
|
"ledger_mileage": sheet.cell(row, 13).value or 0,
|
"platform_mileage": sheet.cell(row, 14).value or 0,
|
"mileage_diff": sheet.cell(row, 15).value or 0,
|
"no_track": sheet.cell(row, 16).value or 0,
|
})
|
elif "\u4f01\u4e1a\u6838\u67e5\u7ed3\u679c" in wb.sheetnames:
|
sheet = wb["\u4f01\u4e1a\u6838\u67e5\u7ed3\u679c"]
|
for row in range(5, sheet.max_row + 1):
|
enterprise = sheet.cell(row, 2).value
|
if not enterprise:
|
continue
|
turnover_pair = str(sheet.cell(row, 4).value or "").split("/")
|
mileage_pair = str(sheet.cell(row, 6).value or "").split("/")
|
if len(turnover_pair) != 2 or len(mileage_pair) != 2:
|
continue
|
enterprises.append({
|
"city": sheet.cell(row, 1).value or "",
|
"enterprise": enterprise,
|
"ledger_vehicles": sheet.cell(row, 3).value or 0,
|
"month_turnover": float(turnover_pair[0].replace(",", "").strip()) * 10000,
|
"ledger_turnover": float(turnover_pair[1].replace(",", "").strip()) * 10000,
|
"report_vs_ledger": sheet.cell(row, 5).value or 0,
|
"ledger_mileage": float(mileage_pair[0].replace(",", "").strip()),
|
"platform_mileage": float(mileage_pair[1].replace(",", "").strip()),
|
"mileage_diff": sheet.cell(row, 7).value or 0,
|
"no_track": sheet.cell(row, 8).value or 0,
|
})
|
no_track_by_enterprise: Dict[str, List[str]] = {}
|
if "\u8f66\u8f86\u91cc\u7a0b\u5dee\u5f02\u60c5\u51b5" in wb.sheetnames:
|
vehicle_sheet = wb["\u8f66\u8f86\u91cc\u7a0b\u5dee\u5f02\u60c5\u51b5"]
|
for values in vehicle_sheet.iter_rows(min_row=4, max_col=12, values_only=True):
|
enterprise = values[1]
|
plate = values[2]
|
status = values[11]
|
if enterprise and plate and status == "\u5e73\u53f0\u660e\u7ec6\u65e0\u5339\u914d":
|
no_track_by_enterprise.setdefault(str(enterprise), []).append(str(plate))
|
for item in enterprises:
|
item["no_track_plates"] = no_track_by_enterprise.get(str(item["enterprise"]), [])
|
wb.close()
|
return enterprises
|
|
|
def turnover_judgment(item: Dict[str, Any]) -> str:
|
difference = float(item["report_vs_ledger"])
|
if abs(difference) <= 0.01:
|
return "\u6570\u503c\u57fa\u672c\u4e00\u81f4"
|
if "\u51ef\u7965\u8fbe" in str(item["enterprise"]):
|
return "\u5468\u8f6c\u91cf\u6c47\u603b\u516c\u5f0f\u9519\u8bef"
|
return "\u6708\u62a5\u53f0\u8d26\u5dee\u5f02\u8f83\u5927"
|
|
|
def mileage_judgment(item: Dict[str, Any]) -> str:
|
difference = float(item["mileage_diff"])
|
if difference > 0.30:
|
parts = ["\u53f0\u8d26\u91cc\u7a0b\u660e\u663e\u9ad8\u4e8e\u5e73\u53f0"]
|
elif difference < -0.30:
|
parts = ["\u53f0\u8d26\u91cc\u7a0b\u660e\u663e\u4f4e\u4e8e\u5e73\u53f0"]
|
else:
|
parts = ["\u53f0\u8d26\u91cc\u7a0b\u5728\u6b63\u5e38\u533a\u95f4"]
|
if int(item["no_track"] or 0) > 0:
|
parts.append("\u5b58\u5728\u65e0\u5e73\u53f0\u6708\u91cc\u7a0b\u8f66\u8f86")
|
return "\uff1b".join(parts)
|
|
|
def add_two_level_table(
|
doc: Document,
|
groups: Sequence[Tuple[str, Sequence[str]]],
|
rows: Sequence[Sequence[str]],
|
widths_cm: Sequence[float],
|
body_alignments: Sequence[str],
|
header_size: float = 9.0,
|
body_size: float = 8.5,
|
) -> Any:
|
column_count = sum(max(1, len(children)) for _, children in groups)
|
if len(widths_cm) != column_count or len(body_alignments) != column_count:
|
raise ValueError("two-level table width or alignment count mismatch")
|
table = doc.add_table(rows=2, cols=column_count)
|
table.alignment = WD_TABLE_ALIGNMENT.CENTER
|
table.style = "Table Grid"
|
table.autofit = False
|
set_table_borders_gray(table)
|
set_table_widths(table, widths_cm)
|
|
top_row = table.rows[0]
|
second_row = table.rows[1]
|
set_repeat_table_header(top_row)
|
set_repeat_table_header(second_row)
|
prevent_row_split(top_row)
|
prevent_row_split(second_row)
|
column = 0
|
for label, children in groups:
|
span = max(1, len(children))
|
if children:
|
top_cell = top_row.cells[column]
|
if span > 1:
|
top_cell = top_cell.merge(top_row.cells[column + span - 1])
|
set_cell_text(top_cell, label, header_size, True, ALIGN_CENTER)
|
for offset, child in enumerate(children):
|
set_cell_text(second_row.cells[column + offset], child, header_size, True, ALIGN_CENTER)
|
else:
|
merged = top_row.cells[column].merge(second_row.cells[column])
|
set_cell_text(merged, label, header_size, True, ALIGN_CENTER)
|
column += span
|
|
for values in rows:
|
row = table.add_row()
|
prevent_row_split(row)
|
for index, value in enumerate(values):
|
set_cell_text(row.cells[index], value, body_size, False, body_alignments[index])
|
set_table_widths(table, widths_cm)
|
for row_index in (0, 1):
|
for cell in table.rows[row_index].cells:
|
set_cell_margins(cell, top=35, start=45, bottom=35, end=45)
|
for paragraph in cell.paragraphs:
|
paragraph.paragraph_format.line_spacing = Pt(12)
|
for row in table.rows[2:]:
|
for cell in row.cells:
|
set_cell_margins(cell, top=35, start=55, bottom=35, end=55)
|
for paragraph in cell.paragraphs:
|
paragraph.paragraph_format.line_spacing = Pt(12)
|
|
spacer = doc.add_paragraph()
|
spacer.paragraph_format.first_line_indent = Pt(0)
|
spacer.paragraph_format.space_before = Pt(0)
|
spacer.paragraph_format.space_after = Pt(0)
|
spacer.paragraph_format.line_spacing = Pt(6)
|
return table
|
|
|
def strict_missing_endpoints(vehicle: Dict[str, Any]) -> List[str]:
|
return [
|
endpoint["label"] for endpoint in vehicle["points"].get("endpoints", [])
|
if endpoint.get("strict", True) and endpoint["day_count"] == 0
|
]
|
|
|
def main_region(vehicle: Dict[str, Any]) -> str:
|
regions: List[str] = []
|
for name, points, days in vehicle["points"].get("top_counties", [])[:2]:
|
parts = [part for part in str(name).split("|") if part]
|
label = (parts[-2] + parts[-1]) if len(parts) >= 3 else (parts[-1] if parts else str(name))
|
regions.append(label)
|
return "\u3001".join(regions) if regions else "\u65e0\u6709\u6548\u6d3b\u52a8\u533a\u57df"
|
|
|
def sample_judgment(vehicle: Dict[str, Any]) -> str:
|
if strict_missing_endpoints(vehicle):
|
return "\u7533\u62a5\u7ebf\u8def\u672a\u83b7\u8f68\u8ff9\u652f\u6301"
|
return "\u7ebf\u8def\u65b9\u5411\u6709\u652f\u6301\u4f46\u8d9f\u6b21\u4e0d\u8db3"
|
|
|
def sample_support(vehicle: Dict[str, Any]) -> float:
|
return float(vehicle["comparison"].get("trip_support_rate") or 0)
|
|
|
def ledger_route_text(vehicle: Dict[str, Any]) -> str:
|
groups = list(vehicle["ledger"].get("route_groups", []))
|
groups.sort(key=lambda group: float(group.get("trips") or 0), reverse=True)
|
parts: List[str] = []
|
for group in groups:
|
route = str(group.get("route") or "").strip()
|
if len(route) > 28:
|
route = route[:27] + "\u2026"
|
trips = float(group.get("trips") or 0)
|
parts.append(f"{route}\uff08{trips:g}\u8d9f\uff09")
|
return "\u3001".join(parts) if parts else "\u672a\u8bc6\u522b"
|
|
|
def build_report(workbook_path: Path, diagnostics_path: Path, output_path: Path) -> None:
|
enterprises = read_enterprises(workbook_path)
|
enterprises.sort(key=lambda item: (city_sort_key(item["city"]), str(item["enterprise"])))
|
diagnostics_data = json.loads(diagnostics_path.read_text(encoding="utf-8"))
|
vehicles = diagnostics_data["vehicles"]
|
if len(enterprises) != 12 or len(vehicles) != 12:
|
raise RuntimeError("Expected 12 enterprise rows and 12 sample vehicles")
|
vehicles_by_enterprise = {str(item["enterprise"]): item for item in vehicles}
|
if any(str(item["enterprise"]) not in vehicles_by_enterprise for item in enterprises):
|
raise RuntimeError("Enterprise and sample vehicle data do not match")
|
|
total_ledger_mileage = sum(float(item["ledger_mileage"]) for item in enterprises)
|
total_platform_mileage = sum(float(item["platform_mileage"]) for item in enterprises)
|
total_vehicles = sum(int(item["ledger_vehicles"]) for item in enterprises)
|
total_no_track = sum(int(item["no_track"]) for item in enterprises)
|
layer1_consistent = sum(abs(float(item["report_vs_ledger"])) <= 0.01 for item in enterprises)
|
layer2_normal = sum(abs(float(item["mileage_diff"])) <= 0.30 for item in enterprises)
|
total_mileage_diff = total_ledger_mileage / total_platform_mileage - 1 if total_platform_mileage else 0
|
no_track_ratio = total_no_track / total_vehicles if total_vehicles else 0
|
raw_points = sum(int(item["points"]["valid_point_count"] or 0) for item in vehicles)
|
point_files = sum(int(item["points"]["file_count"] or 0) for item in vehicles)
|
point_days_30 = sum(int(item["points"]["day_count"] or 0) == 30 for item in vehicles)
|
point_days_29 = len(vehicles) - point_days_30
|
positive_days_30 = sum(int(item["platform"].get("positive_mileage_days") or 0) == 30 for item in vehicles)
|
positive_days_29 = sum(int(item["platform"].get("positive_mileage_days") or 0) == 29 for item in vehicles)
|
positive_days_short = len(vehicles) - positive_days_30 - positive_days_29
|
route_mismatch = sum(bool(strict_missing_endpoints(item)) for item in vehicles)
|
supported_vehicles = [item for item in vehicles if not strict_missing_endpoints(item)]
|
support_below_half_supported = sum(sample_support(item) < 0.5 for item in supported_vehicles)
|
supported_support_min = min(sample_support(item) for item in supported_vehicles)
|
supported_support_max = max(sample_support(item) for item in supported_vehicles)
|
kxd = next((item for item in enterprises if "\u51ef\u7965\u8fbe" in str(item["enterprise"])), None)
|
datong = next((item for item in enterprises if "\u5927\u901a" in str(item["enterprise"])), None)
|
|
doc = Document()
|
configure_document(doc)
|
doc.core_properties.title = "2026\u5e749\u6708\u4f01\u4e1a\u53f0\u8d26\u62bd\u67e5\u6838\u67e5\u62a5\u544a"
|
doc.core_properties.subject = "\u4f01\u4e1a\u53f0\u8d26\u3001\u91cd\u8f7d\u5e73\u53f0\u91cc\u7a0b\u548c\u91cd\u70b9\u8f66\u8f86\u8f68\u8ff9\u70b9\u6838\u67e5"
|
add_title(doc, "2026\u5e749\u6708\u4f01\u4e1a\u53f0\u8d26\u62bd\u67e5\u6838\u67e5\u62a5\u544a")
|
|
add_body(
|
doc,
|
"\u6309\u7167\u5de5\u4f5c\u5b89\u6392\uff0c\u5bf9\u5b9c\u660c\u5e02\u3001\u9ec4\u5188\u5e02\u3001\u54b8\u5b81\u5e023\u4e2a\u5e02\u5dde12\u5bb6\u4f01\u4e1a2026\u5e749\u6708\u53f0\u8d26\u53ca\u5176\u8f66\u8f86\u8f68\u8ff9\u6570\u636e\u5f00\u5c55\u62bd\u67e5\u6838\u67e5\u3002\u73b0\u5c06\u6838\u67e5\u60c5\u51b5\u62a5\u544a\u5982\u4e0b\u3002",
|
)
|
|
add_heading(doc, "\u4e00\u3001\u6838\u67e5\u8303\u56f4\u4e0e\u6570\u636e\u60c5\u51b5")
|
add_body(
|
doc,
|
"\u672c\u6b21\u6838\u67e5\u4ee5\u4f01\u4e1a\u4e0a\u62a5\u6708\u62a5\u3001\u4f01\u4e1a\u8fd0\u8f93\u53f0\u8d26\u3001\u91cd\u8f7d\u5e73\u53f0\u8f66\u8f86\u6708\u5ea6\u91cc\u7a0b\u548c\u8f66\u8f86\u8f68\u8ff9\u70b9\u4e3a\u57fa\u7840\uff0c\u4f9d\u6b21\u6838\u5bf9\u6708\u62a5\u4e0e\u53f0\u8d26\u3001\u53f0\u8d26\u91cc\u7a0b\u4e0e\u5e73\u53f0\u76d1\u6d4b\u91cc\u7a0b\uff0c\u5e76\u5bf9\u6bcf\u6237\u4f01\u4e1a1\u8f86\u91cd\u70b9\u8f66\u8f86\u5f00\u5c55\u6574\u6708\u8f68\u8ff9\u70b9\u7cbe\u67e5\u3002\u6838\u67e5\u6570\u636e\u60c5\u51b5\u5982\u4e0b\u3002",
|
)
|
add_caption(doc, "\u88681 \u6838\u67e5\u8303\u56f4\u4e0e\u6570\u636e\u60c5\u51b5")
|
workload_rows = [
|
("\u6838\u67e5\u8303\u56f4", "3\u4e2a\u5e02\u5dde\u300112\u5bb6\u4f01\u4e1a"),
|
("\u4f01\u4e1a\u53f0\u8d26", "12\u4efd\uff0c\u8f66\u8f86\u2014\u8def\u7ebf\u2014\u8d9f\u6b21\u2014\u8d27\u8fd0\u91cf\u2014\u5468\u8f6c\u91cf\u8bb0\u5f553,003\u6761"),
|
("\u53f0\u8d26\u8f66\u8f86", f"{fmt_int(total_vehicles)}\u8f86"),
|
("\u5e73\u53f0\u6708\u5ea6\u91cc\u7a0b", "12\u4efd\uff0c\u4ece\u91cd\u8f7d\u5e73\u53f0\u4e0b\u8f7d\uff0c\u8f66\u8f86\u65e5\u91cc\u7a0b\u8bb0\u5f5512,921\u6761"),
|
("\u91cd\u70b9\u8f66\u8f86\u7cbe\u67e5", f"\u4ece\u8f66\u8f86\u65e5\u91cc\u7a0b\u8bb0\u5f55\u7b5b\u9009\u8f68\u8ff9\u5c3d\u91cf\u8986\u76d6\u5168\u6708\u7684\u8f66\u8f86\uff0c\u517112\u8f86\uff0c\u6bcf\u6237\u4f01\u4e1a1\u8f86\uff1b{point_days_30}\u8f8630\u5929\u6709\u8f68\u8ff9\u70b9\uff0c{point_days_29}\u8f8629\u5929\u6709\u8f68\u8ff9\u70b9"),
|
("\u8f68\u8ff9\u70b9\u8d44\u6599", f"{point_files}\u4e2a\u6587\u4ef6\uff0c\u4ece\u91cd\u8f7d\u5e73\u53f0\u4e0b\u8f7d\uff0c\u8f68\u8ff9\u70b9{fmt_int(raw_points)}\u4e2a"),
|
("\u95ee\u9898\u6c47\u603b", "\u53e6\u9644\u95ee\u9898\u6c47\u603b\u8868Excel\uff0c\u517140\u6761\u95ee\u9898\u8bb0\u5f55"),
|
]
|
add_table(
|
doc,
|
["\u6838\u67e5\u7ef4\u5ea6", "\u6570\u91cf\u53ca\u5185\u5bb9"],
|
workload_rows,
|
[3.5, 11.1],
|
[ALIGN_CENTER, ALIGN_LEFT],
|
header_size=12,
|
body_size=12,
|
)
|
|
add_heading(doc, "\u4e8c\u3001\u6838\u67e5\u5185\u5bb9\u4e0e\u65b9\u6cd5")
|
add_heading(doc, "\uff08\u4e00\uff09\u5206\u5c42\u6838\u67e5", 2)
|
add_body(
|
doc,
|
"\u7b2c\u4e00\u5c42\u6838\u5bf9\u6708\u62a5\u4e0e\u53f0\u8d26\u5468\u8f6c\u91cf\uff0c\u5dee\u5f02\u7387\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc71%\u7684\u5224\u5b9a\u4e3a\u6570\u503c\u57fa\u672c\u4e00\u81f4\uff0c\u8d85\u8fc71%\u7684\u5217\u4e3a\u5dee\u5f02\u4e8b\u9879\uff1b\u7b2c\u4e8c\u5c42\u9010\u8f66\u6bd4\u5bf9\u53f0\u8d26\u91cc\u7a0b\u4e0e\u5e73\u53f0\u6708\u5ea6\u76d1\u6d4b\u91cc\u7a0b\uff0c\u5dee\u5f02\u7387\u5728\u6b63\u8d1f30%\u4ee5\u5185\u7684\u5224\u5b9a\u4e3a\u6b63\u5e38\uff1b\u7b2c\u4e09\u5c42\u5bf9\u91cd\u70b9\u8f66\u8f86\u6574\u6708\u8f68\u8ff9\u70b9\u6838\u67e5\u6d3b\u52a8\u533a\u57df\u3001\u7ebf\u8def\u7aef\u70b9\u3001\u91cc\u7a0b\u89c4\u6a21\u548c\u53ef\u652f\u6301\u8d9f\u6b21\u3002",
|
)
|
add_heading(doc, "\uff08\u4e8c\uff09\u91cd\u70b9\u8f66\u8f86\u9009\u53d6", 2)
|
add_body(
|
doc,
|
"\u91cd\u70b9\u8f66\u8f86\u4f18\u5148\u9009\u53d6\u5e73\u53f0\u65e5\u91cc\u7a0b30\u5929\u5747\u6709\u8bb0\u5f55\u4e14\u6bcf\u65e5\u603b\u91cc\u7a0b\u5927\u4e8e0\u3001\u53f0\u8d26\u4e0e\u5e73\u53f0\u91cc\u7a0b\u5dee\u5f02\u8d85\u8fc730%\u3001\u7ebf\u8def\u53ef\u4e0e\u53f0\u8d26\u5bf9\u5e94\u3001\u8f68\u8ff9\u70b9\u8fde\u7eed\u6027\u8f83\u597d\u7684\u8f66\u8f86\u3002\u4e2a\u522b\u4f01\u4e1a\u4e0d\u5b58\u5728\u540c\u65f6\u6ee1\u8db3\u4e0a\u8ff0\u6761\u4ef6\u7684\u8f66\u8f86\u65f6\uff0c\u9009\u53d6\u5f53\u6708\u8986\u76d629\u5929\u4ee5\u4e0a\u3001\u5df2\u8986\u76d6\u65e5\u671f\u603b\u91cc\u7a0b\u5747\u5927\u4e8e0\u4e14\u5dee\u5f02\u7387\u8f83\u5927\u7684\u8f66\u8f86\uff0c\u5e76\u5c06\u8986\u76d6\u4e0d\u8db3\u4f5c\u4e3a\u4f01\u4e1a\u52a8\u6001\u76d1\u63a7\u7ef4\u62a4\u95ee\u9898\u5355\u72ec\u6838\u67e5\u3002",
|
)
|
add_heading(doc, "\uff08\u4e09\uff09\u8bc1\u636e\u4f7f\u7528\u8fb9\u754c", 2)
|
add_body(
|
doc,
|
"\u8f68\u8ff9\u6570\u636e\u7528\u4e8e\u5224\u65ad\u8f66\u8f86\u6d3b\u52a8\u533a\u57df\u3001\u7ebf\u8def\u65b9\u5411\u3001\u91cc\u7a0b\u89c4\u6a21\u548c\u8d9f\u6b21\u6570\u91cf\u7ea7\u3002\u8f68\u8ff9\u8986\u76d6\u5929\u6570\u7528\u4e8e\u5224\u65ad\u4f01\u4e1a\u52a8\u6001\u76d1\u63a7\u7ef4\u62a4\u60c5\u51b5\uff0c\u4e0d\u636e\u6b64\u5355\u72ec\u8ba4\u5b9a\u53f0\u8d26\u865a\u5047\uff1b\u8f68\u8ff9\u4e5f\u4e0d\u80fd\u76f4\u63a5\u8bc1\u660e\u6bcf\u8d9f\u8d27\u7269\u5b9e\u9645\u91cd\u91cf\u548c\u88c5\u5378\u8d27\u4e8b\u5b9e\uff0c\u76f8\u5173\u4e8b\u9879\u4ecd\u9700\u7ed3\u5408\u8fd0\u5355\u3001\u8fc7\u78c5\u5355\u3001\u88c5\u5378\u8d27\u8bb0\u5f55\u7b49\u539f\u59cb\u51ed\u636e\u6838\u5b9e\u3002",
|
)
|
|
add_section(doc, landscape=False)
|
add_heading(doc, "\u4e09\u3001\u6838\u67e5\u7ed3\u679c")
|
add_heading(doc, "\uff08\u4e00\uff09\u6708\u62a5\u4e0e\u53f0\u8d26\u5468\u8f6c\u91cf", 2)
|
add_body(
|
doc,
|
f"12\u5bb6\u4f01\u4e1a\u4e2d\uff0c{layer1_consistent}\u5bb6\u6708\u62a5\u4e0e\u53f0\u8d26\u5468\u8f6c\u91cf\u5dee\u5f02\u7387\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc71%\uff0c1\u5bb6\u8d85\u8fc750%\u3002\u5404\u4f01\u4e1a\u6bd4\u8f83\u60c5\u51b5\u89c1\u88682\u3002",
|
)
|
add_caption(doc, "\u88682 \u4f01\u4e1a\u6708\u62a5\u4e0e\u53f0\u8d26\u5468\u8f6c\u91cf\u6838\u67e5\u7ed3\u679c")
|
turnover_groups = [
|
("\u5e8f\u53f7", ()),
|
("\u5e02\u5dde", ()),
|
("\u4f01\u4e1a", ()),
|
("\u5468\u8f6c\u91cf\uff08\u4e07\u5428\u516c\u91cc\uff09", ("\u6708\u62a5", "\u53f0\u8d26", "\u5dee\u5f02\u7387")),
|
("\u6838\u67e5\u5224\u65ad", ()),
|
]
|
turnover_rows: List[Sequence[str]] = []
|
for index, item in enumerate(enterprises, start=1):
|
turnover_rows.append((
|
str(index), short_city_name(item["city"]), str(item["enterprise"]),
|
fmt_wan_ton_km(item["month_turnover"]), fmt_wan_ton_km(item["ledger_turnover"]),
|
fmt_pct(item["report_vs_ledger"]), turnover_judgment(item),
|
))
|
add_two_level_table(
|
doc,
|
turnover_groups,
|
turnover_rows,
|
[0.8, 1.1, 4.0, 2.1, 2.1, 1.5, 3.0],
|
[ALIGN_CENTER, ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_LEFT],
|
header_size=12,
|
body_size=10.5,
|
)
|
|
if kxd:
|
add_body(
|
doc,
|
f"\u5b9c\u660c\u51ef\u7965\u8fbe\u7269\u6d41\u6709\u9650\u516c\u53f89\u6708\u6708\u62a5\u5468\u8f6c\u91cf{fmt_wan_ton_km(kxd['month_turnover'])}\u4e07\u5428\u516c\u91cc\uff0c\u53f0\u8d26\u9010\u884c\u6b63\u786e\u5408\u8ba1{fmt_wan_ton_km(kxd['ledger_turnover'])}\u4e07\u5428\u516c\u91cc\uff0c\u6708\u62a5\u591a\u62a5{fmt_pct(kxd['report_vs_ledger'])}\u3002\u8be5\u4f01\u4e1a1\u20149\u6708\u5747\u4f7f\u7528\u201c\u8d27\u8fd0\u91cf\u5408\u8ba1\u00d7\u5e73\u5747\u91cc\u7a0b\u201d\u8ba1\u7b97\u5468\u8f6c\u91cf\uff0c\u5df2\u67e5\u660e\u4e3a\u516c\u5f0f\u9519\u8bef\u30021\u20149\u6708\u6708\u62a5\u5468\u8f6c\u91cf\u5408\u8ba1{fmt_wan_ton_km(KAIXIANGDA_TOTAL_REPORT)}\u4e07\u5428\u516c\u91cc\uff0c\u53f0\u8d26\u9010\u884c\u6b63\u786e\u5408\u8ba1{fmt_wan_ton_km(KAIXIANGDA_TOTAL_CORRECT)}\u4e07\u5428\u516c\u91cc\uff0c\u7d2f\u8ba1\u591a\u62a5{fmt_wan_ton_km(KAIXIANGDA_TOTAL_OVER)}\u4e07\u5428\u516c\u91cc\uff0c\u591a\u62a5\u7387{fmt_pct(KAIXIANGDA_TOTAL_RATE, False)}\u3002",
|
)
|
add_caption(doc, "\u88683 \u5b9c\u660c\u51ef\u7965\u8fbe1\u20149\u6708\u5468\u8f6c\u91cf\u6c47\u603b\u6838\u67e5\u7ed3\u679c")
|
kxd_rows = [
|
(month, fmt_wan_ton_km(report), fmt_wan_ton_km(correct), fmt_wan_ton_km(over), fmt_pct(rate, False))
|
for month, report, correct, over, rate in KAIXIANGDA_MONTHLY
|
]
|
kxd_rows.append((
|
"\u5408\u8ba1",
|
fmt_wan_ton_km(KAIXIANGDA_TOTAL_REPORT),
|
fmt_wan_ton_km(KAIXIANGDA_TOTAL_CORRECT),
|
fmt_wan_ton_km(KAIXIANGDA_TOTAL_OVER),
|
fmt_pct(KAIXIANGDA_TOTAL_RATE, False),
|
))
|
kxd_table = add_table(
|
doc,
|
["\u6708\u4efd", "\u6708\u62a5\u5468\u8f6c\u91cf\uff08\u4e07\u5428\u516c\u91cc\uff09", "\u53f0\u8d26\u6b63\u786e\u5408\u8ba1\uff08\u4e07\u5428\u516c\u91cc\uff09", "\u7d2f\u8ba1\u591a\u62a5\uff08\u4e07\u5428\u516c\u91cc\uff09", "\u591a\u62a5\u7387"],
|
kxd_rows,
|
[1.3, 3.2, 3.6, 3.3, 2.2],
|
[ALIGN_CENTER, ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_RIGHT],
|
header_size=12,
|
body_size=12,
|
)
|
compact_table(kxd_table)
|
add_body(
|
doc,
|
"\u8be5\u9519\u8bef\u516c\u5f0f\u53ea\u6709\u5728\u5404\u8d9f\u91cc\u7a0b\u6216\u5404\u8d9f\u8d27\u8fd0\u91cf\u5b8c\u5168\u4e00\u81f4\u65f6\u624d\u7b49\u4e8e\u9010\u884c\u5408\u8ba1\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u80fd\u591a\u62a5\u6216\u5c11\u62a5\u3002\u51ef\u7965\u8fbe1\u20149\u6708\u5404\u6708\u5747\u8868\u73b0\u4e3a\u591a\u62a5\uff0c\u9700\u6309\u660e\u7ec6\u9010\u884c\u91cd\u7b97\u5e76\u8c03\u51cf\u3002",
|
)
|
|
add_heading(doc, "\uff08\u4e8c\uff09\u4f01\u4e1a\u53f0\u8d26\u91cc\u7a0b\u4e0e\u5e73\u53f0\u76d1\u6d4b\u91cc\u7a0b", 2)
|
add_body(
|
doc,
|
f"12\u5bb6\u4f01\u4e1a\u53f0\u8d26\u8f66\u8f86\u5171{fmt_int(total_vehicles)}\u8f86\uff0c\u5176\u4e2d\u65e0\u5e73\u53f0\u6708\u91cc\u7a0b\u8bb0\u5f55\u8f66\u8f86{fmt_int(total_no_track)}\u8f86\uff0c\u5360{fmt_pct(no_track_ratio, False)}\u3002\u53f0\u8d26\u91cc\u7a0b\u5408\u8ba1{fmt_num(total_ledger_mileage, 0)}\u516c\u91cc\uff0c\u5e73\u53f0\u76d1\u6d4b\u91cc\u7a0b\u5408\u8ba1{fmt_num(total_platform_mileage, 0)}\u516c\u91cc\uff0c\u53f0\u8d26\u8f83\u5e73\u53f0\u9ad8{fmt_pct(total_mileage_diff)}\u3002\u6309\u6b63\u8d1f30%\u7684\u6b63\u5e38\u533a\u95f4\u5224\u65ad\uff0c{layer2_normal}\u5bb6\u4f01\u4e1a\u5904\u4e8e\u6b63\u5e38\u8303\u56f4\uff0c{len(enterprises) - layer2_normal}\u5bb6\u4f01\u4e1a\u8d85\u51fa\u6b63\u5e38\u8303\u56f4\u3002",
|
)
|
if datong:
|
add_body(
|
doc,
|
f"\u6e56\u5317\u5927\u901a\u4e34\u7a7a\u7269\u6d41\u6709\u9650\u516c\u53f8\u4f01\u4e1a\u53f0\u8d26\u4e0e\u5e73\u53f0\u91cc\u7a0b\u5dee\u5f02\u7387\u4e3a{fmt_pct(datong['mileage_diff'])}\uff0c\u5904\u4e8e\u6b63\u5e38\u8303\u56f4\uff1b\u5176\u4f5911\u5bb6\u4f01\u4e1a\u5747\u8d85\u51fa\u6b63\u5e38\u8303\u56f4\u3002",
|
)
|
|
add_section(doc, landscape=True)
|
add_caption(doc, "\u88684 \u4f01\u4e1a\u53f0\u8d26\u91cc\u7a0b\u4e0e\u5e73\u53f0\u76d1\u6d4b\u91cc\u7a0b\u6838\u67e5\u7ed3\u679c")
|
mileage_groups = [
|
("\u5e8f\u53f7", ()),
|
("\u5e02\u5dde", ()),
|
("\u4f01\u4e1a", ()),
|
("\u53f0\u8d26\u8f66\u8f86\u6570", ()),
|
("\u91cc\u7a0b\uff08\u516c\u91cc\uff09", ("\u53f0\u8d26", "\u5e73\u53f0", "\u5dee\u5f02\u7387")),
|
("\u65e0\u8f68\u8ff9\u8f66\u8f86", ("\u6570\u91cf", "\u5360\u6bd4")),
|
("\u6838\u67e5\u5224\u65ad", ()),
|
]
|
mileage_rows: List[Sequence[str]] = []
|
for index, item in enumerate(enterprises, start=1):
|
ledger_vehicles = int(item["ledger_vehicles"] or 0)
|
no_track_vehicles = int(item["no_track"] or 0)
|
no_track_ratio_item = no_track_vehicles / ledger_vehicles if ledger_vehicles else 0
|
mileage_rows.append((
|
str(index), short_city_name(item["city"]), str(item["enterprise"]), fmt_int(ledger_vehicles),
|
fmt_num(item["ledger_mileage"], 0), fmt_num(item["platform_mileage"], 0), fmt_pct(item["mileage_diff"]),
|
fmt_int(no_track_vehicles), fmt_pct(no_track_ratio_item, False), mileage_judgment(item),
|
))
|
add_two_level_table(
|
doc,
|
mileage_groups,
|
mileage_rows,
|
[0.8, 1.1, 4.8, 1.4, 2.0, 2.0, 1.6, 1.4, 1.6, 9.0],
|
[ALIGN_CENTER, ALIGN_CENTER, ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_RIGHT,
|
ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_LEFT],
|
header_size=12,
|
body_size=10.5,
|
)
|
|
add_section(doc, landscape=False)
|
add_heading(doc, "\uff08\u4e09\uff09\u91cd\u70b9\u8f66\u8f86\u8f68\u8ff9\u70b9\u7cbe\u67e5", 2)
|
add_body(
|
doc,
|
f"12\u8f86\u91cd\u70b9\u8f66\u8f86\u5171\u6838\u9a8c\u8f68\u8ff9\u70b9{fmt_int(raw_points)}\u4e2a\u3002{route_mismatch}\u8f86\u7533\u62a5\u7ebf\u8def\u672a\u83b7\u8f68\u8ff9\u652f\u6301\uff1b\u5176\u4f59{len(vehicles) - route_mismatch}\u8f86\u7ebf\u8def\u65b9\u5411\u867d\u6709\u8f68\u8ff9\u652f\u6301\uff0c\u4f46\u53ef\u652f\u6301\u8d9f\u6b21\u6bd4\u4f8b\u5747\u672a\u8fbe\u5230100%\uff0c\u8303\u56f4{fmt_pct(supported_support_min, False)}\u2014{fmt_pct(supported_support_max, False)}\uff0c\u5176\u4e2d{support_below_half_supported}\u8f86\u4f4e\u4e8e50%\u3002{positive_days_30}\u8f86\u5e73\u53f0\u65e5\u91cc\u7a0b30\u5929\u603b\u91cc\u7a0b\u5747\u5927\u4e8e0\uff0c{positive_days_29}\u8f86\u4e3a29\u5929\uff0c\u53e6\u6709{positive_days_short}\u8f86\u4ec523\u5929\u603b\u91cc\u7a0b\u5927\u4e8e0\uff0c\u52a8\u6001\u76d1\u6d4b\u7ef4\u62a4\u5b58\u5728\u7f3a\u53e3\u3002\u8f68\u8ff9\u53ef\u652f\u6301\u8d9f\u6b21\u6bd4\u4f8b\u4ec5\u5bf9\u7ebf\u8def\u65b9\u5411\u83b7\u5f97\u8f68\u8ff9\u652f\u6301\u7684{len(vehicles) - route_mismatch}\u8f86\u8f66\u8ba1\u7b97\u3002",
|
)
|
add_section(doc, landscape=True)
|
add_caption(doc, "\u88685 \u91cd\u70b9\u8f66\u8f86\u8f68\u8ff9\u6838\u67e5\u7ed3\u679c")
|
sample_groups = [
|
("\u5e8f\u53f7", ()),
|
("\u5e02\u5dde", ()),
|
("\u4f01\u4e1a", ()),
|
("\u8f66\u724c\u53f7", ()),
|
("\u53f0\u8d26", ("\u8d9f\u6b21", "\u91cc\u7a0b\uff08\u516c\u91cc\uff09")),
|
("\u5e73\u53f0\u76d1\u6d4b", ("\u91cc\u7a0b\uff08\u516c\u91cc\uff09", "\u8f68\u8ff9\u70b9\u6570", "\u91cc\u7a0b\u5927\u4e8e0\u5929\u6570")),
|
("\u53f0\u8d26\u8f83\u5e73\u53f0\u91cc\u7a0b\u5dee\u5f02\u7387", ()),
|
("\u8f68\u8ff9\u53ef\u652f\u6301\u8d9f\u6b21\u6bd4\u4f8b", ()),
|
("\u6838\u67e5\u5224\u65ad", ()),
|
]
|
sample_rows: List[Sequence[str]] = []
|
for index, item in enumerate(enterprises, start=1):
|
vehicle = vehicles_by_enterprise[str(item["enterprise"])]
|
sample_rows.append((
|
str(index), short_city_name(item["city"]), str(item["enterprise"]), str(vehicle["plate"]),
|
fmt_num(vehicle["ledger"]["trips"], 0), fmt_num(vehicle["ledger"]["mileage"], 2),
|
fmt_num(vehicle["platform"]["total_mileage"], 2), fmt_int(vehicle["points"]["valid_point_count"]),
|
str(int(vehicle["platform"].get("positive_mileage_days") or 0)),
|
fmt_pct(vehicle["comparison"].get("ledger_vs_platform_rate")), ("\u2014" if strict_missing_endpoints(vehicle) else fmt_pct(sample_support(vehicle), False)),
|
sample_judgment(vehicle),
|
))
|
add_two_level_table(
|
doc,
|
sample_groups,
|
sample_rows,
|
[0.8, 1.1, 4.2, 1.8, 1.2, 1.7, 1.7, 1.8, 1.5, 2.0, 2.2, 5.7],
|
[ALIGN_CENTER, ALIGN_CENTER, ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_RIGHT,
|
ALIGN_RIGHT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, ALIGN_LEFT],
|
header_size=12,
|
body_size=10.5,
|
)
|
|
add_section(doc, landscape=False)
|
add_heading(doc, "\u56db\u3001\u4f01\u4e1a\u95ee\u9898\u6c47\u603b")
|
add_body(
|
doc,
|
"\u4f01\u4e1a\u9010\u9879\u6838\u67e5\u95ee\u9898\u3001\u6d89\u53ca\u8f66\u8f86\u3001\u53f0\u8d26\u60c5\u51b5\u3001\u6708\u62a5\u53ca\u5e73\u53f0\u76d1\u6d4b\u60c5\u51b5\u3001\u6838\u67e5\u7ed3\u8bba\u548c\u6838\u67e5\u4f9d\u636e\uff0c\u8be6\u89c1\u9644\u4ef6\u300a2026\u5e749\u6708\u4f01\u4e1a\u53f0\u8d26\u62bd\u67e5\u6838\u67e5\u95ee\u9898\u6c47\u603b\u8868.xlsx\u300b\u3002",
|
)
|
|
add_heading(doc, "\u4e94\u3001\u5904\u7406\u5efa\u8bae")
|
add_body(
|
doc,
|
"\u8981\u6c42\u5b9c\u660c\u51ef\u7965\u8fbe\u7269\u6d41\u6709\u9650\u516c\u53f8\u7acb\u5373\u5c06\u5468\u8f6c\u91cf\u6c47\u603b\u516c\u5f0f\u6539\u4e3a\u9010\u884c\u201c\u8d27\u8fd0\u91cf\u00d7\u8be5\u8d9f\u91cc\u7a0b\u201d\u6c42\u548c\uff0c\u5e76\u5bf91\u20149\u6708\u591a\u62a5\u7684\u6570\u636e\u62ff\u51fa\u8c03\u51cf\u65b9\u6848\u3002",
|
"\uff08\u4e00\uff09",
|
)
|
add_body(
|
doc,
|
"\u5bf92026\u5e74636\u5bb6\u89c4\u4e0a\u8d27\u8fd0\u4f01\u4e1a\u7ec4\u7ec7\u5f00\u5c55\u5468\u8f6c\u91cf\u6c47\u603b\u516c\u5f0f\u4e13\u9879\u7b5b\u67e5\uff0c\u9010\u6237\u4ee5\u660e\u7ec6\u884c\u91cd\u7b971\u20149\u6708\u53ca\u7d2f\u8ba1\u5468\u8f6c\u91cf\uff0c\u65e2\u8bc6\u522b\u591a\u62a5\uff0c\u4e5f\u8bc6\u522b\u5c11\u62a5\u3002",
|
"\uff08\u4e8c\uff09",
|
)
|
add_body(
|
doc,
|
"\u5bf9\u53f0\u8d26\u91cc\u7a0b\u4e0e\u5e73\u53f0\u91cc\u7a0b\u5dee\u5f02\u8d85\u8fc730%\u768411\u5bb6\u4f01\u4e1a\uff0c\u8981\u6c42\u9010\u8f66\u8bf4\u660e\u539f\u56e0\uff0c\u5e76\u63d0\u4f9b\u8fd0\u8f93\u4efb\u52a1\u6e90\u3001\u8fd0\u5355\u3001\u8fc7\u78c5\u5355\u3001\u88c5\u5378\u8d27\u8bb0\u5f55\u548c\u8fd0\u8f93\u5408\u540c\u7b49\u539f\u59cb\u8d44\u6599\u3002",
|
"\uff08\u4e09\uff09",
|
)
|
add_body(
|
doc,
|
"\u5bf964\u8f86\u65e0\u5e73\u53f0\u6708\u91cc\u7a0b\u8bb0\u5f55\u8f66\u8f86\u5f00\u5c55\u4e13\u9879\u6e05\u7406\uff0c\u9010\u8f66\u6838\u5b9e\u63a5\u5165\u72b6\u6001\u3001\u8f66\u724c\u4fe1\u606f\u3001\u8fd0\u8425\u72b6\u6001\u548c\u53f0\u8d26\u586b\u62a5\u60c5\u51b5\uff1b\u5bf9\u5e73\u53f0\u603b\u91cc\u7a0b\u5927\u4e8e0\u5929\u6570\u4e0d\u8db330\u5929\u7684\u91cd\u70b9\u8f66\u8f86\uff0c\u6838\u5b9e\u8f66\u8f7d\u7ec8\u7aef\u5728\u7ebf\u548c\u8fd0\u884c\u72b6\u6001\u3002",
|
"\uff08\u56db\uff09",
|
)
|
add_body(
|
doc,
|
"\u5bf9\u7533\u62a5\u7ebf\u8def\u672a\u83b7\u8f68\u8ff9\u652f\u6301\u3001\u7ebf\u8def\u7aef\u70b9\u51fa\u73b0\u9891\u6b21\u4e0d\u8db3\u7684\u8f66\u8f86\uff0c\u7ed3\u5408\u8c03\u5ea6\u8bb0\u5f55\u3001\u88c5\u5378\u8d27\u5730\u70b9\u3001\u9ad8\u901f\u516c\u8def\u901a\u884c\u8bb0\u5f55\u7b49\u5f00\u5c55\u590d\u6838\u3002",
|
"\uff08\u4e94\uff09",
|
)
|
add_body(
|
doc,
|
"\u7763\u4fc3\u4f01\u4e1a\u89c4\u8303\u5efa\u7acb\u6708\u5ea6\u8fd0\u8f93\u53f0\u8d26\uff0c\u505a\u5230\u8f66\u8f86\u3001\u65e5\u671f\u3001\u7ebf\u8def\u3001\u8d9f\u6b21\u3001\u91cc\u7a0b\u3001\u8d27\u8fd0\u91cf\u548c\u5468\u8f6c\u91cf\u76f8\u4e92\u5bf9\u5e94\uff1b\u540c\u6b65\u843d\u5b9e\u8f66\u8f86\u52a8\u6001\u76d1\u63a7\u8bbe\u5907\u7ef4\u62a4\u548c\u5f02\u5e38\u6570\u636e\u5904\u7f6e\u8d23\u4efb\u3002",
|
"\uff08\u516d\uff09",
|
)
|
|
signature = doc.add_paragraph()
|
signature.alignment = WD_ALIGN_PARAGRAPH.RIGHT
|
signature.paragraph_format.first_line_indent = Pt(0)
|
signature.paragraph_format.space_before = Pt(18)
|
signature.paragraph_format.space_after = Pt(0)
|
signature.paragraph_format.line_spacing = Pt(30)
|
signature_run = signature.add_run("2026\u5e7410\u670829\u65e5")
|
set_run_font(signature_run, BODY_FONT, 16, False)
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
doc.save(output_path)
|
|
|
def main() -> int:
|
parser = argparse.ArgumentParser(description=__doc__)
|
parser.add_argument("--workbook", required=True)
|
parser.add_argument("--diagnostics", required=True)
|
parser.add_argument("--output", required=True)
|
args = parser.parse_args()
|
workbook = Path(args.workbook)
|
diagnostics = Path(args.diagnostics)
|
output = Path(args.output)
|
if not workbook.is_file():
|
raise SystemExit("workbook not found: %s" % workbook)
|
if not diagnostics.is_file():
|
raise SystemExit("diagnostics not found: %s" % diagnostics)
|
build_report(workbook, diagnostics, output)
|
print(str(output))
|
return 0
|
|
|
if __name__ == "__main__":
|
raise SystemExit(main())
|