在更新一版,更方便了
This commit is contained in:
@@ -90,15 +90,20 @@ class PurchaseOrderMerger:
|
||||
|
||||
def get_purchase_orders(self) -> List[str]:
|
||||
"""
|
||||
获取output目录下的采购单Excel文件
|
||||
获取result目录下的采购单Excel文件
|
||||
|
||||
Returns:
|
||||
采购单文件路径列表
|
||||
"""
|
||||
logger.info(f"搜索目录 {self.output_dir} 中的采购单Excel文件")
|
||||
# 采购单文件保存在data/result目录
|
||||
result_dir = "data/result"
|
||||
logger.info(f"搜索目录 {result_dir} 中的采购单Excel文件")
|
||||
|
||||
# 确保目录存在
|
||||
os.makedirs(result_dir, exist_ok=True)
|
||||
|
||||
# 获取所有Excel文件
|
||||
all_files = get_files_by_extensions(self.output_dir, ['.xls', '.xlsx'])
|
||||
all_files = get_files_by_extensions(result_dir, ['.xls', '.xlsx'])
|
||||
|
||||
# 筛选采购单文件
|
||||
purchase_orders = [
|
||||
@@ -107,7 +112,7 @@ class PurchaseOrderMerger:
|
||||
]
|
||||
|
||||
if not purchase_orders:
|
||||
logger.warning(f"未在 {self.output_dir} 目录下找到采购单Excel文件")
|
||||
logger.warning(f"未在 {result_dir} 目录下找到采购单Excel文件")
|
||||
return []
|
||||
|
||||
# 按修改时间排序,最新的在前
|
||||
@@ -394,9 +399,11 @@ class PurchaseOrderMerger:
|
||||
# 采购单价(必填)- E列(4)
|
||||
output_sheet.write(r, price_col, float(row['采购单价']), price_style)
|
||||
|
||||
# 生成输出文件名
|
||||
# 生成输出文件名,保存到data/result目录
|
||||
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
output_file = os.path.join(self.output_dir, f"合并采购单_{timestamp}.xls")
|
||||
result_dir = "data/result"
|
||||
os.makedirs(result_dir, exist_ok=True)
|
||||
output_file = os.path.join(result_dir, f"合并采购单_{timestamp}.xls")
|
||||
|
||||
# 保存文件
|
||||
output_workbook.save(output_file)
|
||||
@@ -443,4 +450,4 @@ class PurchaseOrderMerger:
|
||||
self.merged_files[file_path] = output_file
|
||||
self._save_merged_files()
|
||||
|
||||
return output_file
|
||||
return output_file
|
||||
@@ -671,9 +671,11 @@ class ExcelProcessor:
|
||||
logger.warning("未提取到有效商品信息")
|
||||
return None
|
||||
|
||||
# 生成输出文件名
|
||||
# 生成输出文件名,保存到data/result目录
|
||||
file_name = os.path.splitext(os.path.basename(file_path))[0]
|
||||
output_file = os.path.join(self.output_dir, f"采购单_{file_name}.xls")
|
||||
result_dir = "data/result"
|
||||
os.makedirs(result_dir, exist_ok=True)
|
||||
output_file = os.path.join(result_dir, f"采购单_{file_name}.xls")
|
||||
|
||||
# 填充模板并保存
|
||||
if self.fill_template(products, output_file):
|
||||
@@ -944,4 +946,4 @@ class ExcelProcessor:
|
||||
except Exception as e:
|
||||
logger.warning(f"解析规格'{spec_str}'时出错: {e}")
|
||||
|
||||
return None
|
||||
return None
|
||||
@@ -127,4 +127,67 @@ class OCRService:
|
||||
Returns:
|
||||
图片是否有效
|
||||
"""
|
||||
return self.ocr_processor.validate_image(image_path)
|
||||
return self.ocr_processor.validate_image(image_path)
|
||||
|
||||
def _is_valid_image(self, image_path: str) -> bool:
|
||||
"""
|
||||
检查文件是否为有效的图片格式
|
||||
|
||||
Args:
|
||||
image_path: 图片文件路径
|
||||
|
||||
Returns:
|
||||
是否为有效图片格式
|
||||
"""
|
||||
return self.validate_image(image_path)
|
||||
|
||||
def _get_excel_path(self, image_path: str) -> str:
|
||||
"""
|
||||
根据图片路径生成对应的Excel文件路径
|
||||
|
||||
Args:
|
||||
image_path: 图片文件路径
|
||||
|
||||
Returns:
|
||||
Excel文件路径
|
||||
"""
|
||||
# 获取文件名(不含扩展名)
|
||||
base_name = os.path.splitext(os.path.basename(image_path))[0]
|
||||
# 生成Excel文件路径
|
||||
output_dir = self.config.get('Paths', 'output_folder', fallback='data/output')
|
||||
excel_path = os.path.join(output_dir, f"{base_name}.xlsx")
|
||||
return excel_path
|
||||
|
||||
def _generate_excel(self, ocr_result: dict, image_path: str) -> Optional[str]:
|
||||
"""
|
||||
根据OCR结果生成Excel文件
|
||||
|
||||
Args:
|
||||
ocr_result: OCR识别结果
|
||||
image_path: 原始图片路径
|
||||
|
||||
Returns:
|
||||
生成的Excel文件路径,失败返回None
|
||||
"""
|
||||
try:
|
||||
excel_path = self._get_excel_path(image_path)
|
||||
|
||||
# 确保输出目录存在
|
||||
os.makedirs(os.path.dirname(excel_path), exist_ok=True)
|
||||
|
||||
# 调用OCR处理器的Excel生成功能
|
||||
if hasattr(self.ocr_processor, 'generate_excel'):
|
||||
success = self.ocr_processor.generate_excel(ocr_result, excel_path)
|
||||
if success:
|
||||
return excel_path
|
||||
else:
|
||||
# 如果OCR处理器没有generate_excel方法,直接返回路径
|
||||
# 假设OCR处理器已经生成了Excel文件
|
||||
if os.path.exists(excel_path):
|
||||
return excel_path
|
||||
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"生成Excel文件时发生错误: {e}", exc_info=True)
|
||||
return None
|
||||
@@ -37,7 +37,10 @@ class TobaccoService:
|
||||
# 修复配置获取方式,使用fallback机制
|
||||
self.output_dir = config.get('Paths', 'output_folder', fallback='data/output')
|
||||
self.template_file = config.get('Paths', 'template_file', fallback='templates/银豹-采购单模板.xls')
|
||||
self.output_file = os.path.join(self.output_dir, '银豹采购单_烟草公司.xls')
|
||||
# 将烟草订单保存到result目录
|
||||
result_dir = "data/result"
|
||||
os.makedirs(result_dir, exist_ok=True)
|
||||
self.output_file = os.path.join(result_dir, '银豹采购单_烟草公司.xls')
|
||||
|
||||
def get_latest_tobacco_order(self) -> Optional[str]:
|
||||
"""
|
||||
@@ -261,4 +264,4 @@ class TobaccoService:
|
||||
)
|
||||
|
||||
# 记录日志
|
||||
logger.info(f"烟草公司订单处理成功,订单时间: {order_time}, 总金额: {total_amount}, 处理条目: {total_count}")
|
||||
logger.info(f"烟草公司订单处理成功,订单时间: {order_time}, 总金额: {total_amount}, 处理条目: {total_count}")
|
||||
Reference in New Issue
Block a user