feat: 新增交通银行数据导出插件及测试通道机制

This commit is contained in:
2026-11-05 15:20:34 +08:00
parent b976b80d9d
commit 4e8b3e7d43
230 changed files with 19827 additions and 1440 deletions
@@ -515,10 +515,27 @@ def _to_text(value):
def _clean_value(value):
"""None/NaN 归一为 '',其余原样(用于写入单元格)。"""
"""归一单元格取值:None/NaN/NaT → '',numpy 标量 → Python 原生类型。
xlwt 的 ws.write 只接受 str/int/float/bool/Decimal/datetime/None,
直接写 numpy.int64 会抛 `Unexpected data type <class 'numpy.int64'>`,
因此这里统一做一次类型归零(pandas 读表得到的都是 numpy 标量)。
"""
if value is None:
return ''
import math
try:
import numpy as np
if isinstance(value, np.generic): # numpy.int64 / float64 / bool_ 等
value = value.item()
except ImportError:
pass
try:
import pandas as pd
if pd.isna(value): # NaN / NaT / pd.NA
return ''
except (TypeError, ValueError, ImportError):
pass
if isinstance(value, float) and math.isnan(value):
return ''
return value
@@ -554,14 +571,52 @@ def _id_hit(cid, ref_ids):
return bool(cid) and cid in ref_ids
def _xlsx_valid(path):
"""校验 .xlsx 是否有效(OOXML 是 ZIP 包,文件头应为 PK\\x03\\x04)。
返回 (是否有效, 说明)。常见无效原因:0 KB 空文件、把 .xls 直接改名成 .xlsx、
文件损坏、或正被 Excel/WPS 独占写入。
"""
try:
size = os.path.getsize(path)
except OSError as e:
return False, f"无法读取:{e}"
if size == 0:
return False, "空文件(0 KB)"
try:
with open(path, "rb") as f:
head = f.read(4)
except OSError as e:
return False, f"无法读取:{e}"
if head != b"PK\x03\x04":
return False, f"非有效 xlsx(文件头不是 ZIP,{size / 1024:.0f} KB," \
f"可能是 .xls 改名/损坏/被占用)"
return True, f"{size / 1024:.0f} KB"
def _latest_file(directory, pattern):
"""目录内按模式取最新文件(排除 Excel 临时文件 ~$)。"""
"""目录内按模式取最新文件(排除 Excel 临时文件 ~$)。
.xlsx 额外做有效性校验:最新候选不是有效 xlsx 时直接报错,**不静默回退**
到历史月份名单(否则会按旧数据生成导入名单、回写错办理状态)。
"""
from glob import glob
candidates = [p for p in glob(os.path.join(directory, pattern))
if not os.path.basename(p).startswith("~$")]
if not candidates:
raise FileNotFoundError(f"在 {directory} 下未找到匹配 {pattern} 的文件")
return max(candidates, key=os.path.getmtime)
candidates.sort(key=os.path.getmtime, reverse=True)
if pattern.lower().endswith(".xlsx"):
ok, detail = _xlsx_valid(candidates[0])
if not ok:
others = ";".join(f"{os.path.basename(p)}({_xlsx_valid(p)[1]})"
for p in candidates[1:6]) or "无"
raise ValueError(
f"最新文件不是有效的 xlsx:{os.path.basename(candidates[0])}({detail})"
f";同目录其它候选:{others}。"
f"请确认该文件是否为 0 KB 空文件、是否把 .xls 直接改名成 .xlsx、"
f"是否正被 Excel/WPS 打开(用「另存为 → Excel 工作簿(*.xlsx)」重存后重试)")
return candidates[0]
def _pick_sheet(xl, exact_name, keyword, pattern=None):