前言

在很多公司或组织中,每天/每周/每月都需要整理报表并发送给相关人员。这个流程如果手动操作,不仅耗时耗力,还容易出错。而使用 Python,我们可以自动生成报表 + 自动发送邮件,彻底解放双手!

本文将带你完整实现一个企业级办公自动化脚本,涵盖:

  1. 报表数据生成(使用 pandas + openpyxl)

  2. 报表文件保存为 Excel

  3. 通过邮箱将报表发送给指定收件人


一、环境准备

安装所需依赖:


bash

复制编辑

pip install pandas openpyxl yagmail

其中:

  • pandas 用于处理数据

  • openpyxl 用于写入 Excel

  • yagmail 简化邮件发送(基于 SMTP)


二、生成模拟报表数据(pandas)


python

复制编辑

import pandas as pd from datetime import datetime import random # 构建模拟销售数据 def generate_sales_data(): data = [] for i in range(1, 21): # 模拟20条记录 data.append({ "日期": datetime.now().strftime("%Y-%m-%d"), "订单号": f"ODR{1000+i}", "客户": random.choice(["张三", "李四", "王五"]), "商品": random.choice(["鼠标", "键盘", "耳机"]), "数量": random.randint(1, 10), "单价": random.randint(50, 200), }) df = pd.DataFrame(data) df["总价"] = df["数量"] * df["单价"] return df


三、将报表保存为 Excel 文件(openpyxl 引擎)


python

复制编辑

def save_to_excel(df, filename="销售报表.xlsx"): df.to_excel(filename, index=False, engine='openpyxl') print(f"报表已保存为:{filename}")

调用:


python

复制编辑

df = generate_sales_data() save_to_excel(df)


四、发送邮件(附带 Excel 报表)

1. 配置邮箱账号(建议使用 163、QQ、Gmail 等开启 SMTP)

使用 yagmail 登录:


python

复制编辑

import yagmail # 第一次用推荐使用 yagmail.register(email, password) 进行加密保存 # 启用SMTP(以163邮箱为例) sender_email = "your_email@163.com" sender_password = "你的授权码" receiver_email = "target@example.com"


2. 构建发送逻辑


python

复制编辑

def send_email(report_path, receiver): subject = "销售日报 - 自动发送" body = "您好,请查收今日销售报表,附件为Excel文件。" yag = yagmail.SMTP(user=sender_email, password=sender_password, host='smtp.163.com') yag.send(to=receiver, subject=subject, contents=body, attachments=report_path) print("邮件发送成功!")


五、完整自动化脚本(整合所有步骤)


python

复制编辑

def main(): df = generate_sales_data() filename = f"销售报表_{datetime.now().strftime('%Y%m%d')}.xlsx" save_to_excel(df, filename) send_email(filename, receiver_email) if __name__ == "__main__": main()

运行效果:

  • 自动生成一份 Excel 报表(含随机销售数据)

  • 自动发送至指定邮箱,附件即为报表


六、结合定时任务实现“每日自动发报表”

配合 APScheduler(参考上一篇):


python

复制编辑

from apscheduler.schedulers.blocking import BlockingScheduler scheduler = BlockingScheduler() scheduler.add_job(main, 'cron', hour=9, minute=0) # 每天上午9点发送报表 scheduler.start()


七、安全提示 ⚠️

  • 邮箱密码建议使用“授权码”而非明文密码

  • 可将敏感信息保存在 .env 或配置文件中读取

  • 发送大量邮件建议设置频率限制,避免被视为垃圾邮件


八、进一步扩展建议

  • 数据来源接入数据库 / Excel / API

  • Excel 美化:加边框、加颜色、合并单元格(使用 openpyxl)

  • 邮件内容支持 HTML 模板(用于发送图文邮件)

  • 多人群发 / 抄送 / 密送支持


总结

步骤 工具/模块
生成数据 pandas + random
写入 Excel pandas.to_excel
发邮件 yagmail + SMTP
定时执行 APScheduler

通过这些组件组合,你可以轻松搭建起自己的 企业自动化日报系统,提升效率,减少重复劳动。

https://bigu.wang

https://www.bigu.wang

https://binm.wang

https://www.binm.wang

https://bint.wang

https://www.bint.wang

https://biop.wang

https://www.biop.wang

https://bits.wang

https://www.bits.wang

https://bjqb.wang

https://www.bjqb.wang

https://bjsm.wang

https://www.bjsm.wang

https://bleo.wang

https://www.bleo.wang

https://ono.wang

https://www.ono.wang

https://onz.wang

https://www.onz.wang

https://opo.wang

https://www.opo.wang

https://osm.wang

https://www.osm.wang

https://osn.wang

https://www.osn.wang

https://ovi.wang

https://www.ovi.wang

https://oxq.wang

https://www.oxq.wang

https://oti.wang

https://www.oti.wang

https://owu.wang

https://www.owu.wang

https://piq.wang

https://www.piq.wang

https://qmi.wang

https://www.qmi.wang

https://qki.wang

https://www.qki.wang

https://ref.wang

https://www.ref.wang

https://sak.wang

https://www.sak.wang

https://sar.wang

https://www.sar.wang

https://sfa.wang

https://www.sfa.wang

https://sfe.wang

https://www.sfe.wang

https://sgo.wang

https://www.sgo.wang

https://sku.wang

https://www.sku.wang

https://ycxjz.cn

https://www.ycxjz.cn

https://bnbmhomes.cn

https://www.bnbmhomes.cn

https://jinjianzuche.com

https://www.jinjianzuche.com

https://ahswt.cn

https://www.ahswt.cn

https://szwandaj.cn

https://www.szwandaj.cn

https://psbest.cn

https://www.psbest.cn

https://shanghai-arnold.cn

https://www.shanghai-arnold.cn

https://zgsscw.com

https://www.zgsscw.com

https://shxqth.cn

https://www.shxqth.cn

https://wdxj.cn

https://www.wdxj.cn

https://jad168.com

https://www.jad168.com

https://ultratrailms.cn

https://www.ultratrailms.cn

https://tztsjd.cn

https://www.tztsjd.cn

https://csqcbx.cn

https://www.csqcbx.cn

https://qazit.cn

https://www.qazit.cn

https://ahzjyl.cn

https://www.ahzjyl.cn

Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐