Download instock_automate_warning_function.py from pengyuyan1222/instock_warning_GUI_cloud: direct link, hf CLI and curl.
- Browser
- Download file 4.94 kB
-
https://huggingface.co/spaces/pengyuyan1222/instock_warning_GUI_cloud/resolve/main/instock_automate_warning_function.py
- Command line
-
hf download hf://spaces/pengyuyan1222/instock_warning_GUI_cloud/instock_automate_warning_function.py
-
curl -L -o instock_automate_warning_function.py https://huggingface.co/spaces/pengyuyan1222/instock_warning_GUI_cloud/resolve/main/instock_automate_warning_function.py
4.94 kB
| import pandas as pd | |
| from sqlalchemy import create_engine | |
| import xlwings as xw | |
| from urllib import parse | |
| import smtplib | |
| from email.mime.multipart import MIMEMultipart | |
| from email.mime.text import MIMEText | |
| from email.mime.base import MIMEBase | |
| from email import encoders | |
| import os | |
| import time | |
| # 一.获取每日最新订单数据 | |
| # 1.1.连接数据库函数 | |
| def connect_database(user, password, host, database, port): | |
| encoded_password = parse.quote_plus(password) | |
| connection_string = f"mysql+pymysql://{user}:{encoded_password}@{host}:{port}/{database}" | |
| engine = create_engine(connection_string,echo=True) | |
| return engine | |
| # 1.2.定义获取数据的函数 | |
| def fetch_data(sql, engine): | |
| get_df = pd.read_sql(sql, con=engine) | |
| get_df_without_index_and_header = get_df.to_numpy() | |
| return get_df_without_index_and_header | |
| # 二、源数据更新 | |
| # 2.1.定义数据更新脚本函数 √ | |
| def update_origin_data(file_path_model_order_data): | |
| app = xw.App(visible=False) | |
| # 打开目标Excel文件 | |
| for file_path_model,order_data_last_60_days in file_path_model_order_data.items(): | |
| wb = xw.Book(file_path_model) | |
| # 选择要操作的工作表 | |
| target_sheet_name = '官网库存预警-订单源数据' | |
| target_sheet = wb.sheets[target_sheet_name] | |
| # 取消筛选状态 | |
| if target_sheet.api.AutoFilterMode: | |
| target_sheet.api.AutoFilterMode = False # 关闭筛选模式 | |
| # 清空从第二行A列至第100000行N列的数据,保留格式和公式 | |
| target_sheet.range('A2:N100000').clear_contents() | |
| # 将数据粘贴至目标表的A2单元格 | |
| target_sheet.range('A2').value = order_data_last_60_days | |
| # 保存&关闭文件 | |
| wb.save() | |
| print('保存休眠20s') | |
| time.sleep(20) | |
| wb.close() | |
| print('关闭休眠10s') | |
| time.sleep(10) | |
| # 三.发送预警邮件 | |
| # 3.1.定义缺货情况信息获取函数 √ | |
| def lack_of_stock_SKU_count(file_path_model_target_sheet): | |
| # 信息配置 | |
| app = xw.App(visible=False) | |
| lack_of_stock_SKU_dict = {} | |
| # 打开预警模型 | |
| for file_path_model,target_sheet_name_list in file_path_model_target_sheet.items(): | |
| wb = xw.Book(file_path_model) | |
| for target_sheet_name in target_sheet_name_list: | |
| target_sheet = wb.sheets[target_sheet_name] | |
| lack_of_stock_SKU_count = target_sheet.range('B5').value #获取【补货SKU数】单元格数值 | |
| lack_of_stock_SKU_dict[target_sheet_name] = lack_of_stock_SKU_count | |
| print(f'{target_sheet_name}近3天预计补货SKU数:{lack_of_stock_SKU_count}') | |
| wb.close() | |
| return lack_of_stock_SKU_dict | |
| # 3.2.定义邮件发送函数 √ | |
| def send_email_qq(subject,body,to_recipients,cc_recipients=None,bcc_recipients=None,attachments=None): | |
| smtp_server = "smtp.qq.com" # QQ SMTP 服务器 | |
| smtp_port = 465 # 使用 SSL | |
| sender_email = "2352756059@qq.com" # 发件人邮箱 | |
| sender_password = "fbpxerqyyxkldhia" # QQ邮箱授权码 | |
| # 创建邮件对象 | |
| msg = MIMEMultipart() | |
| msg["From"] = sender_email | |
| # msg["To"] = to_recipients | |
| # msg["CC"] = cc_recipients if cc_recipients else "" | |
| msg['To'] = ", ".join(to_recipients) # 逗号分隔字符串 | |
| msg['Cc'] = ", ".join(cc_recipients) # 逗号分隔字符串 | |
| msg["Subject"] = subject | |
| # 添加邮件正文 | |
| msg.attach(MIMEText(body, "plain", "utf-8")) | |
| # 添加附件 | |
| if attachments: | |
| for file_path in attachments: | |
| with open(file_path, "rb") as attachment: | |
| part = MIMEBase("application", "octet-stream") | |
| part.set_payload(attachment.read()) | |
| encoders.encode_base64(part) | |
| part.add_header("Content-Disposition", f"attachment; filename={os.path.basename(file_path)}") | |
| msg.attach(part) | |
| # 发送邮件 | |
| try: | |
| server = smtplib.SMTP_SSL(smtp_server, smtp_port) # 使用 SSL 连接 | |
| server.login(sender_email, sender_password) # 登录 | |
| server.sendmail(sender_email, to_recipients, msg.as_string()) # 发送邮件 | |
| server.quit() | |
| print("✅ 邮件发送成功!") | |
| except Exception as e: | |
| print(f"❌ 发送失败: {e}") | |
| # 3.3.定义邮件内容编写函数 √ | |
| def email_body_write(lack_of_stock_SKU_dict,model_url_dict): | |
| email_body_lines = [] | |
| for site,sku_count in lack_of_stock_SKU_dict.items(): | |
| if sku_count == 0.0: | |
| email_body_lines.append(f'{site}近3天不存在需要补货的SKU') | |
| else: | |
| email_body_lines.append(f'{site}近3天存在{sku_count}个SKU需补货,快去检查下吧') | |
| email_body_lines.append('\n') | |
| for name,url in model_url_dict.items(): | |
| email_body_lines.append(f'{name}:{url}') | |
| email_body = "\n".join(email_body_lines) | |
| return email_body | |
| if __name__ == '__main__': | |
| pass |