每个逻辑站点一个排队任务,与采集共用并行数量;工参按拆除前后规则取本系统快照,指标暂用模拟数据;全部站点结束后写频段清退、低效拆除两份结果表。去掉原来的通用导出任务。
209 lines
7.8 KiB
Python
209 lines
7.8 KiB
Python
"""在固定模板上就地扩行、删行、套样式和合并单元格。
|
|
|
|
openpyxl 自带的 insert_rows / delete_rows 不移动合并区、行高和条件格式,模板版式会乱,这里自己移动。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
from collections.abc import Callable
|
|
|
|
from openpyxl.cell.cell import MergedCell
|
|
from openpyxl.formatting.formatting import ConditionalFormattingList
|
|
from openpyxl.styles import Alignment, Font, PatternFill
|
|
from openpyxl.utils import get_column_letter
|
|
from openpyxl.utils.cell import range_boundaries
|
|
|
|
ZEBRA = PatternFill(fill_type="solid", start_color="F2F6FC", end_color="F2F6FC")
|
|
MISSING = PatternFill(fill_type="solid", start_color="FFC7CE", end_color="FFC7CE")
|
|
|
|
|
|
def text(ws, row: int, col: int) -> str:
|
|
value = ws.cell(row=row, column=col).value
|
|
return str(value).strip() if value is not None else ""
|
|
|
|
|
|
def find_row(ws, test: Callable[[int], bool], start: int = 1, stop: int | None = None) -> int | None:
|
|
for row in range(start, (stop or ws.max_row) + 1):
|
|
if test(row):
|
|
return row
|
|
return None
|
|
|
|
|
|
def header_map(ws, row: int) -> dict[str, int]:
|
|
found: dict[str, int] = {}
|
|
for col in range(1, ws.max_column + 1):
|
|
label = text(ws, row, col)
|
|
if label:
|
|
found.setdefault(label, col)
|
|
return found
|
|
|
|
|
|
def row_empty(ws, row: int, last_col: int) -> bool:
|
|
return all(text(ws, row, col) == "" for col in range(1, last_col + 1))
|
|
|
|
|
|
def block_end(ws, first: int, last_col: int) -> int:
|
|
"""从 first 起连续的非空行,返回最后一行。"""
|
|
row = first
|
|
while row + 1 <= ws.max_row and not row_empty(ws, row + 1, last_col):
|
|
row += 1
|
|
return row
|
|
|
|
|
|
def _map_row(row: int, at: int, delta: int) -> int | None:
|
|
if delta > 0:
|
|
return row + delta if row >= at else row
|
|
end = at - delta
|
|
if row < at:
|
|
return row
|
|
if row < end:
|
|
return None
|
|
return row + delta
|
|
|
|
|
|
def _map_span(first: int, last: int, at: int, delta: int) -> tuple[int, int] | None:
|
|
if delta > 0:
|
|
return (first + delta if first >= at else first), (last + delta if last >= at else last)
|
|
end = at - delta
|
|
if first >= at and last < end:
|
|
return None
|
|
new_first = first if first < at else (at if first < end else first + delta)
|
|
new_last = last if last < at else (at - 1 if last < end else last + delta)
|
|
return (new_first, new_last) if new_last >= new_first else None
|
|
|
|
|
|
def remap_conditional(ws, mapper: Callable[[int, int], tuple[int, int] | None]) -> None:
|
|
entries = [(str(item.sqref), list(item.rules)) for item in ws.conditional_formatting]
|
|
if not entries:
|
|
return
|
|
fresh = ConditionalFormattingList()
|
|
for sqref, rules in entries:
|
|
parts = []
|
|
for part in sqref.split():
|
|
min_col, min_row, max_col, max_row = range_boundaries(part)
|
|
span = mapper(min_row, max_row)
|
|
if span is not None:
|
|
parts.append(f"{get_column_letter(min_col)}{span[0]}:{get_column_letter(max_col)}{span[1]}")
|
|
for rule in rules if parts else []:
|
|
fresh.add(" ".join(parts), rule)
|
|
ws.conditional_formatting = fresh
|
|
|
|
|
|
def shift_rows(ws, at: int, delta: int) -> None:
|
|
"""delta > 0 时在第 at 行前插入 delta 行;delta < 0 时从第 at 行起删掉 -delta 行。"""
|
|
if delta == 0:
|
|
return
|
|
merges = [(item.min_row, item.min_col, item.max_row, item.max_col) for item in ws.merged_cells.ranges]
|
|
for item in list(ws.merged_cells.ranges):
|
|
ws.unmerge_cells(str(item))
|
|
cells = ws._cells
|
|
if delta < 0:
|
|
for key in [key for key in cells if at <= key[0] < at - delta]:
|
|
del cells[key]
|
|
start = at if delta > 0 else at - delta
|
|
for key in sorted((key for key in cells if key[0] >= start), reverse=delta > 0):
|
|
cell = cells.pop(key)
|
|
cell.row = key[0] + delta
|
|
cells[(cell.row, key[1])] = cell
|
|
heights = {row: (dim.height, dim.hidden) for row, dim in ws.row_dimensions.items()}
|
|
for row in list(ws.row_dimensions.keys()):
|
|
del ws.row_dimensions[row]
|
|
for row, (height, hidden) in heights.items():
|
|
moved = _map_row(row, at, delta)
|
|
if moved is None:
|
|
continue
|
|
dim = ws.row_dimensions[moved]
|
|
if height is not None:
|
|
dim.height = height
|
|
if hidden:
|
|
dim.hidden = True
|
|
for min_row, min_col, max_row, max_col in merges:
|
|
span = _map_span(min_row, max_row, at, delta)
|
|
if span is None or (span[0] == span[1] and min_col == max_col):
|
|
continue
|
|
ws.merge_cells(start_row=span[0], start_column=min_col, end_row=span[1], end_column=max_col)
|
|
remap_conditional(ws, lambda first, last: _map_span(first, last, at, delta))
|
|
|
|
|
|
def fit_block(ws, first: int, last: int, want: int) -> int:
|
|
"""把 first..last 这段调整成 want 行,返回新的最后一行。扩出的行样式由调用方再套。"""
|
|
want = max(want, 1)
|
|
have = last - first + 1
|
|
if want > have:
|
|
shift_rows(ws, last + 1, want - have)
|
|
remap_conditional(ws, lambda lo, hi: (lo, first + want - 1) if lo >= first and hi == last else (lo, hi))
|
|
elif want < have:
|
|
shift_rows(ws, first + want, want - have)
|
|
return first + want - 1
|
|
|
|
|
|
def unmerge_within(ws, first: int, last: int) -> None:
|
|
for item in [str(item) for item in ws.merged_cells.ranges if item.min_row >= first and item.max_row <= last]:
|
|
ws.unmerge_cells(item)
|
|
|
|
|
|
def row_style(ws, row: int, last_col: int) -> tuple[list, float | None]:
|
|
styles = []
|
|
for col in range(1, last_col + 1):
|
|
cell = ws._cells.get((row, col))
|
|
styles.append(copy.copy(cell._style) if cell is not None and not isinstance(cell, MergedCell) else None)
|
|
return styles, ws.row_dimensions[row].height
|
|
|
|
|
|
def apply_style(ws, row: int, style: tuple[list, float | None]) -> None:
|
|
styles, height = style
|
|
for col, value in enumerate(styles, start=1):
|
|
if value is not None:
|
|
ws.cell(row=row, column=col)._style = copy.copy(value)
|
|
if height:
|
|
ws.row_dimensions[row].height = height
|
|
|
|
|
|
def clear_row(ws, row: int, last_col: int) -> None:
|
|
for col in range(1, last_col + 1):
|
|
cell = ws._cells.get((row, col))
|
|
if cell is not None and not isinstance(cell, MergedCell):
|
|
cell.value = None
|
|
|
|
|
|
def put(ws, row: int, col: int, value, number_format: str | None = None) -> None:
|
|
"""写值;填进去的数据不加粗(模板示例行有的带粗体)。"""
|
|
cell = ws.cell(row=row, column=col)
|
|
if isinstance(cell, MergedCell):
|
|
return
|
|
cell.value = value
|
|
if number_format:
|
|
cell.number_format = number_format
|
|
font = cell.font
|
|
if font is not None and font.bold:
|
|
cell.font = Font(
|
|
name=font.name,
|
|
size=font.size,
|
|
bold=False,
|
|
italic=font.italic,
|
|
underline=font.underline,
|
|
strike=font.strike,
|
|
vertAlign=font.vertAlign,
|
|
color=copy.copy(font.color),
|
|
)
|
|
|
|
|
|
def merge_down(ws, col: int, first: int, last: int, value, *, wrap: bool = False, border_from=None) -> None:
|
|
"""纵向合并 first..last;合并区的边框取左上角单元格,所以先设边框再合并。"""
|
|
anchor = ws.cell(row=first, column=col)
|
|
if border_from is not None:
|
|
anchor.border = copy.copy(border_from)
|
|
anchor.value = value
|
|
anchor.alignment = Alignment(horizontal="center", vertical="center", wrap_text=wrap)
|
|
if last > first:
|
|
ws.merge_cells(start_row=first, start_column=col, end_row=last, end_column=col)
|
|
|
|
|
|
def fill_rows(ws, first: int, last: int, last_col: int, fill: PatternFill) -> None:
|
|
for row in range(first, last + 1):
|
|
for col in range(1, last_col + 1):
|
|
cell = ws._cells.get((row, col))
|
|
if cell is not None and not isinstance(cell, MergedCell):
|
|
cell.fill = fill
|