Commit 10770790 by zhouhaibao

fix(data-process): 解决数据处理中的依赖包缺失和文件解析错误

- 在 clean_budget_achievement.py 中添加对 openpyxl 样式警告的过滤
- 在 consumables_parser.py 中添加 xlrd 库的导入检查和错误提示
- 为 consumables_parser.py 中的 Excel 文件解析添加异常处理机制
- 在 processor.py 中为数据清洗任务添加依赖包缺失的捕获和日志记录
1 parent 4c486434
...@@ -45,6 +45,13 @@ import tempfile ...@@ -45,6 +45,13 @@ import tempfile
import traceback import traceback
from datetime import datetime from datetime import datetime
from urllib.parse import urlparse from urllib.parse import urlparse
import warnings
warnings.filterwarnings(
'ignore',
message='Workbook contains no default style, apply openpyxl\'s default',
category=UserWarning,
module='openpyxl.styles.stylesheet',
)
import pymysql import pymysql
import requests import requests
......
...@@ -72,6 +72,14 @@ def _detect_engine(path): ...@@ -72,6 +72,14 @@ def _detect_engine(path):
if head[:2] == b'PK': if head[:2] == b'PK':
return 'openpyxl' return 'openpyxl'
if head[:4] == b'\xd0\xcf\x11\xe0': if head[:4] == b'\xd0\xcf\x11\xe0':
try:
import xlrd
except ImportError:
raise ImportError(
'读取 .xls 格式的门店资产耗材文件需要 xlrd 库。\n'
'请运行: pip install xlrd>=2.0.1\n'
'或联系运维安装缺失的依赖包。'
)
return 'xlrd' return 'xlrd'
return None return None
...@@ -99,7 +107,14 @@ class ConsumablesDetailParser: ...@@ -99,7 +107,14 @@ class ConsumablesDetailParser:
list[dict] list[dict]
""" """
engine = _detect_engine(self.file_path) engine = _detect_engine(self.file_path)
df = pd.read_excel(self.file_path, engine=engine, header=0, dtype=str) try:
df = pd.read_excel(self.file_path, engine=engine, header=0, dtype=str)
except ImportError:
raise
except Exception as e:
raise RuntimeError(
f'解析门店资产耗材文件失败(引擎={engine}, 文件={self.file_path}): {e}'
) from e
details = [] details = []
for _, row in df.iterrows(): for _, row in df.iterrows():
store_name = str(_cell(row, '门店名称')).strip() store_name = str(_cell(row, '门店名称')).strip()
......
...@@ -534,12 +534,19 @@ class DataProcessor: ...@@ -534,12 +534,19 @@ class DataProcessor:
logger.info(f"[清洗前置] 任务 {task.id} ({task.month}) 开始运行预算达成数据清洗") logger.info(f"[清洗前置] 任务 {task.id} ({task.month}) 开始运行预算达成数据清洗")
conn = get_conn() conn = get_conn()
try: try:
clean_process_task( try:
conn, clean_process_task(
{'id': task.id, 'month': task.month, conn,
'data_table_type': task.data_table_type}, {'id': task.id, 'month': task.month,
dry_run=False, 'data_table_type': task.data_table_type},
) dry_run=False,
)
except (ImportError, ModuleNotFoundError) as e:
logger.error(
f"[清洗前置] 任务 {task.id} ({task.month}) 缺少必要的依赖包: {e}\n"
f"请在终端运行: pip install -r requirements.txt"
)
raise
finally: finally:
conn.close() conn.close()
# 清洗走独立连接并已提交;刷新 ORM 会话缓存,避免读到旧快照 # 清洗走独立连接并已提交;刷新 ORM 会话缓存,避免读到旧快照
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!