编辑代码
数据管理
字段字典
代码管理
服务发布
编辑:dashboard/main_split.py
/home/ubuntu/app/dashboard/main_split.py
"""「五量七率」总表拆块解析。 ## 为什么需要单独一个模块 WPS 的两张总表(QQ线索表10.2 / ICAR五量七率0930)都是**一个 sheet 里堆了多块表**: 区块1 顾问级:序号 | 销售姓名 | 无效线索 | 有效线索 | 总线索 | 线索有效率 | 线索进店数量 | 线索进店率 | 线索成交数量 | 线索成交率 | 试驾量 | 试驾率 | 订单量 | 订单率 ... '' | 总计 | ... (空行) 区块2 标题行:QQ10月线索来源分析表 表头:序号 | 唯一线索来源 | 无效线索 | 有效线索 | 总线索 | ... ... 各渠道 ... '' | 总计 | ... 之前 app.py 里的 `_find_block_split` 只做了「切掉区块2」,目的是避免线索量被 两边重复计算。但**区块2 的数据本身有用**:它就是「分渠道线索量 + 进店 + 成交」, 而区块1 逐人的那几列就是「销售顾问的线索量/到店量/成交量」。 所以这里把两块都解析出来,供: · 首页「销售顾问」板块(item3) · 首页「分渠道线索/成交」板块(item4) · 到店成交率 / 线索成交率(item5,分子分母同源,避免两个口径打架) · 渠道剔除(item7,从总计里减掉被剔除渠道的量) ⚠️ 严格按表头文字找列,不按固定下标:QQ 与 iCAR 两张表的列顺序并不完全一致。 """ from typing import Any, Dict, List, Optional # ---- 语义列 → 候选表头(按优先级)---- COL_NAME = ("销售姓名", "顾问", "归属人", "销售顾问") COL_CHANNEL = ("唯一线索来源", "线索来源", "来源渠道", "一级来源", "来源") COL_TOTAL = ("总线索", "线索总量", "线索量") COL_VALID = ("有效线索",) COL_INVALID = ("无效线索",) COL_VISITS = ("线索进店数量", "进店数量", "到店数量", "到店量") COL_DEALS = ("线索成交数量", "成交数量", "成交量") COL_TEST = ("试驾量",) COL_ORDERS = ("订单量",) TOTAL_WORDS = ("总计", "合计", "小计") def _s(v: Any) -> str: return str(v if v is not None else "").strip() def _num(v: Any) -> Optional[float]: """宽容取数:'1,234' / '0.5' / '#DIV/0!' / '' / '--' 都要能处理。""" s = _s(v).replace(",", "").replace("%", "") if s in ("", "--", "-", "#DIV/0!", "#VALUE!", "#N/A", "None", "null"): return None if s.startswith("'") and s[1:].replace(".", "", 1).isdigit(): s = s[1:] try: return float(s) except ValueError: return None def _idx_of(header: List[str], cands) -> Optional[int]: for want in cands: for i, h in enumerate(header): if _s(h) == want: return i # 退一步:包含关系(表头常带「(人)」之类的后缀) for i, h in enumerate(header): if want and want in _s(h): return i return None def _pick(row: List, i: Optional[int]) -> Any: if i is None or i < 0 or i >= len(row): return None return row[i] def _is_total_row(row: List) -> bool: return any(_s(c) in TOTAL_WORDS for c in row[:4]) def _is_blank(row: List) -> bool: return not any(_s(c) for c in row) def _header_index(row: List, key_candidates) -> Optional[int]: """该行是不是表头(含有 key 列名),返回 key 所在列。""" for cand in key_candidates: for i, c in enumerate(row[:6]): if _s(c) == cand: return i return None def _read_block(rows: List[List], start: int, name_cands, header: Optional[List] = None) -> Dict[str, Any]: """从 start(表头行的下一行)开始读一个块,直到空行/总计/下一个表头。 header 必须显式传入:区块1 的表头在数据集 `columns` 里(不在 rows 里), 只有区块2 的表头才是 rows 里的一行。 """ if start >= len(rows): return {"items": [], "total": {}, "end": start} if header is None: header = rows[start - 1] if start > 0 else rows[0] ci = { "name": _idx_of(header, name_cands) if name_cands else 0, "channel": _idx_of(header, COL_CHANNEL), "total": _idx_of(header, COL_TOTAL), "valid": _idx_of(header, COL_VALID), "invalid": _idx_of(header, COL_INVALID), "visits": _idx_of(header, COL_VISITS), "deals": _idx_of(header, COL_DEALS), "test": _idx_of(header, COL_TEST), "orders": _idx_of(header, COL_ORDERS), } items, total, i = [], {}, start while i < len(rows): rr = rows[i] if _is_blank(rr): i += 1 continue if _is_total_row(rr): total = { "total": _num(_pick(rr, ci["total"])) or 0, "valid": _num(_pick(rr, ci["valid"])) or 0, "invalid": _num(_pick(rr, ci["invalid"])) or 0, "visits": _num(_pick(rr, ci["visits"])) or 0, "deals": _num(_pick(rr, ci["deals"])) or 0, "test": _num(_pick(rr, ci["test"])) or 0, "orders": _num(_pick(rr, ci["orders"])) or 0, } i += 1 break # 又遇到表头(多块堆叠)→ 本块结束 if _header_index(rr, name_cands or COL_CHANNEL) is not None: break name = _s(_pick(rr, ci["name"] if ci["channel"] is None else ci["channel"])) if not name: i += 1 continue items.append({ "name": name, "total": _num(_pick(rr, ci["total"])) or 0, "valid": _num(_pick(rr, ci["valid"])) or 0, "invalid": _num(_pick(rr, ci["invalid"])) or 0, "visits": _num(_pick(rr, ci["visits"])) or 0, "deals": _num(_pick(rr, ci["deals"])) or 0, "test": _num(_pick(rr, ci["test"])) or 0, "orders": _num(_pick(rr, ci["orders"])) or 0, }) i += 1 if not total and items: total = {k: sum(x.get(k) or 0 for x in items) for k in ("total", "valid", "invalid", "visits", "deals", "test", "orders")} return {"items": items, "total": total, "end": i, "cols": ci} def parse_main(cols: List[Any], rows: List[List]) -> Dict[str, Any]: """解析一张总表 → {consultants, channels, total, channel_total}。 返回空结构表示"这张表不是总表",调用方应按普通表处理。 """ out = {"consultants": [], "channels": [], "total": {}, "channel_total": {}, "ok": False} if not rows: return out hdr = [_s(c) for c in (cols or [])] has_name = _idx_of(hdr, COL_NAME) is not None # —— 区块1:顾问级(表头就是 cols 本身)—— cons = {} if has_name: cons = _read_block(rows, 0, COL_NAME, header=hdr) # —— 区块2:渠道分析(表头藏在 rows 里,靠"唯一线索来源"定位)—— ch = {"items": [], "total": {}} for i, r in enumerate(rows): if _header_index(r, COL_CHANNEL) is not None: ch = _read_block(rows, i + 1, None) break out["consultants"] = [x for x in (cons.get("items") or []) if x["name"] not in TOTAL_WORDS] out["channels"] = [x for x in (ch.get("items") or []) if x["name"] not in TOTAL_WORDS] out["total"] = cons.get("total") or {} out["channel_total"] = ch.get("total") or {} out["ok"] = bool(out["consultants"] or out["channels"]) return out def sum_channels(parsed: Dict[str, Any], names) -> Dict[str, float]: """被剔除渠道的合计(用于从总计里减掉)。""" want = {str(x).strip() for x in (names or []) if str(x).strip()} acc = {"total": 0.0, "visits": 0.0, "deals": 0.0, "valid": 0.0, "invalid": 0.0} if not want: return acc for c in parsed.get("channels") or []: if c["name"] in want: for k in acc: acc[k] += c.get(k) or 0 return acc
保存到服务器
取消