Commit f6fa9c2b by zhouhaibao

feat(data-report): 添加Excel自动表头识别功能

- 新增_read_excel_with_auto_header函数,支持自动扫描前5行找到包含关键词的表头行
- 修改parse_inshop_delivery_touch函数,使用自动表头识别替代固定header=1
- 修改园区店交车触客表解析逻辑,统一使用自动表头识别功能
- 更新日志输出,显示实际识别的表头行索引和列映射关系
- 优化错误处理,当未找到有效表头行时返回None并记录警告日志
1 parent f47bba70
......@@ -994,6 +994,31 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie
return result
def _read_excel_with_auto_header(file_path, primary_col_keywords):
"""
读取 Excel,自动扫描前 5 行找到正确的表头行。
判断依据:该行包含 primary_col_keywords 中的任意一个关键词(作为子串)。
返回 (df, header_row_idx);找不到则 header_row_idx=None 返回 (None, None)。
"""
try:
raw = pd.read_excel(file_path, header=None)
except Exception as e:
logger.warning(f"[_read_excel_auto_header] 读取失败 {file_path}: {e}")
return None, None
for i in range(min(5, len(raw))):
row_vals = [str(c).strip() if c is not None else '' for c in raw.iloc[i].tolist()]
hit = any(any(kw in v for v in row_vals if v) for kw in primary_col_keywords)
if hit:
df = pd.read_excel(file_path, header=i)
logger.info(f"[_read_excel_auto_header] 自动选定 header={i}, 列={[str(c).strip() for c in df.columns.tolist()]}")
return df, i
logger.warning(f"[_read_excel_auto_header] 前 5 行均未命中关键词 {primary_col_keywords}")
return None, None
def parse_inshop_delivery_touch(file_path):
"""
解析店中店交车触客表(区域运营填写)。
......@@ -1002,14 +1027,12 @@ def parse_inshop_delivery_touch(file_path):
返回 dict[store_name] = {...}
"""
logger.info(f"[店中店交车触客表] 解析 {file_path}")
try:
df = pd.read_excel(file_path, header=1)
except Exception:
logger.warning(f"[店中店交车触客表] 读取失败 {file_path}")
df, header_idx = _read_excel_with_auto_header(file_path, ['门店名称', '门店名'])
if df is None:
logger.warning("[店中店交车触客表] 未找到有效表头行")
return {}
df.columns = [str(c).strip() if c else '' for c in df.columns]
logger.info(f"[店中店交车触客表] Excel原始列={df.columns.tolist()}")
col_map = match_columns(df.columns, {
'store_name': ['门店名称', '门店名'],
'sales_order_cnt': ['销售大定数', '销售大定'],
......@@ -1021,7 +1044,7 @@ def parse_inshop_delivery_touch(file_path):
'aftersales_touch_cnt': ['售后触客数', '售后触客'],
'purchase_indicator': ['采买', '兜底指标', '采买兜底'],
})
logger.info(f"[店中店交车触客表] 列映射={col_map}")
logger.info(f"[店中店交车触客表] header={header_idx}, 列映射={col_map}")
if 'store_name' not in col_map:
logger.warning("[店中店交车触客表] 未找到门店名列")
......@@ -1216,10 +1239,9 @@ def parse_park_delivery_touch(file_path, biz_type='1.1'):
同门店同业务类型多行时,数值字段取**平均**。
"""
logger.info(f"[园区店交车触客表] 解析 {file_path}, biz_type={biz_type}")
try:
df = pd.read_excel(file_path, header=1)
except Exception:
logger.warning(f"[园区店交车触客表] 读取失败 {file_path}")
df, header_idx = _read_excel_with_auto_header(file_path, ['门店名称', '门店名'])
if df is None:
logger.warning("[园区店交车触客表] 未找到有效表头行")
return {}
df.columns = [str(c).strip() if c else '' for c in df.columns]
......@@ -1231,9 +1253,7 @@ def parse_park_delivery_touch(file_path, biz_type='1.1'):
'arrival_invalid_cnt': ['到店无效', '无效客户数', '到店无效客户'],
'purchase_indicator': ['采买', '兜底指标', '采买兜底'],
})
if 'store_name' not in col_map:
logger.warning("[园区店交车触客表] 未找到门店名列")
return {}
logger.info(f"[园区店交车触客表] header={header_idx}, 列映射={col_map}")
numeric = ['delivery_cnt', 'arrival_cnt', 'arrival_invalid_cnt']
text = ['purchase_indicator']
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!