上传文件至 /

烟草公司订单下载到下载目录,自动检测今天下载的单个文件,处理生成进货单之后,删除源文件,生成桌面“进货单-生成”的xls文件,然后导入银豹
This commit is contained in:
侯欢 2025-04-16 10:25:46 +08:00
commit 26d80eafa8

96
烟草公司.py Normal file
View File

@ -0,0 +1,96 @@
import os, glob, datetime
import pandas as pd
import xlrd
from xlutils.copy import copy
from openpyxl import load_workbook # 用于读取 .xlsx 的 H1、H3 信息
# --------------------------- 配置路径 ---------------------------
downloads_dir = r"D:\Downloads"
template_file = r"C:\Users\Administrator\Desktop\银豹-采购单模板.xls"
output_file = r"C:\Users\Administrator\Desktop\银豹采购单_生成版.xls"
# --------------------------- 获取今日开始时间戳 ---------------------------
today = datetime.date.today()
today_start = datetime.datetime.combine(today, datetime.time.min).timestamp()
# --------------------------- 查找今日的订单明细文件 ---------------------------
file_pattern = os.path.join(downloads_dir, "订单明细202504*.xlsx")
candidates = glob.glob(file_pattern)
target_file = None
for file in candidates:
if os.path.getctime(file) >= today_start:
target_file = file
break
if not target_file:
print("!!!注意:未查找到今日生成的订单明细文件。")
exit()
print(f"1.✅ 已找到文件:{target_file}")
# --------------------------- 读取订单时间H1与总金额H3 ---------------------------
try:
wb_info = load_workbook(target_file, data_only=True)
ws_info = wb_info.active
order_time = ws_info["H1"].value or "(空)"
total_amount = ws_info["H3"].value or 0
except Exception as e:
print(f"❌ 读取订单信息出错H1/H3{e}")
order_time = "(读取失败)"
total_amount = "(读取失败)"
# --------------------------- 读取订单数据 ---------------------------
columns = ['商品', '盒码', '条码', '建议零售价', '批发价', '需求量', '订单量', '金额']
try:
df_old = pd.read_excel(target_file, header=None, skiprows=3, names=columns)
except Exception as e:
print(f"❌ 读取订单数据失败:{e}")
exit()
# 过滤订单量不为0的数据并计算采购量和单价
df_old = df_old[df_old['订单量'] != 0].copy()
df_old['采购量'] = df_old['订单量'] * 10
df_old['采购单价'] = df_old['金额'] / df_old['采购量']
df_old = df_old.reset_index(drop=True)
# --------------------------- 打开模板,准备写入 ---------------------------
try:
template_rd = xlrd.open_workbook(template_file, formatting_info=True)
template_wb = copy(template_rd)
template_ws = template_wb.get_sheet(0)
# 获取模板中的表头列索引
header_row = template_rd.sheet_by_index(0).row_values(0)
barcode_col = header_row.index("条码(必填)")
amount_col = header_row.index("采购量(必填)")
gift_col = header_row.index("赠送量")
price_col = header_row.index("采购单价(必填)")
# 写入数据到模板
for i, row in df_old.iterrows():
template_ws.write(i + 1, barcode_col, row['盒码']) # 商品条码
template_ws.write(i + 1, amount_col, int(row['采购量'])) # 采购量
template_ws.write(i + 1, gift_col, "") # 赠送量为空
template_ws.write(i + 1, price_col, round(row['采购单价'], 2)) # 采购单价保留两位小数
# 保存输出文件
template_wb.save(output_file)
print(f"2.✅ 文件生成成功:{output_file}")
except Exception as e:
print(f"2.❌ 写入模板失败:{e}")
exit()
# --------------------------- 输出处理结果 ---------------------------
print(f"3.📌 共处理条目数:{len(df_old)}")
print(f" 🕒 订单时间:{order_time}")
print(f" 💰 订单总金额:{total_amount}")
# --------------------------- 删除源文件 ---------------------------
try:
os.remove(target_file)
print(f"4.🗑️ 已删除源文件:{target_file}")
except Exception as e:
print(f"4.⚠️ 删除源文件失败:{e}")