""" 本地文件导入模块 将下载的 Excel 文件直接导入 SaleShow 的 uploads 目录 """ import os import shutil import logging from datetime import datetime logger = logging.getLogger(__name__) def import_excel_file(source_path, upload_dir='uploads'): """ 将 Excel 文件导入 SaleShow 的 uploads 目录 Args: source_path: 源文件路径 upload_dir: SaleShow 的上传目录路径 Returns: str: 导入后的文件名(带时间戳前缀),失败返回 None """ try: if not os.path.exists(source_path): logger.error(f"源文件不存在: {source_path}") return None # 确保上传目录存在 os.makedirs(upload_dir, exist_ok=True) # 生成带时间戳的文件名(与 SaleShow 手动上传的命名规则一致) original_name = os.path.basename(source_path) timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_') new_filename = timestamp + original_name dest_path = os.path.join(upload_dir, new_filename) # 复制文件 shutil.copy2(source_path, dest_path) logger.info(f"文件已导入: {source_path} -> {dest_path}") return new_filename except Exception as e: logger.error(f"导入文件失败: {e}") return None def cleanup_download(filepath): """清理下载的临时文件""" try: if filepath and os.path.exists(filepath): os.remove(filepath) logger.info(f"已清理临时文件: {filepath}") except Exception as e: logger.warning(f"清理临时文件失败: {e}")