Python3, pandas automatically processes exlce data and yagmail emails are sent automatically

Use pandas to realize the statistical extraction of data in the excel table, and the function of mail sending!

Use pandas to process Excel data

We have already shared how pandas reads excel. If you are not familiar with it, you can refer to this article by Xiaoyu, pandas 5 lines of code to read Excel!

So next, what we have to do is to count the data in the excel table and the data that a person in charge is responsible for.
1. Let's take a look at the content of the excel table:
data statistics table.xlsx
Insert picture description here
2. We use code to implement the statistics of information for "Zhang San" and "Li Si"

# -*- coding:utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-8-19
"""
import pandas as pd
import  os
from send_email import send_email   #导入send_email

#定义数据文件的地址 ,这里写两种
#一种:手动输入文件地址
excel_path = input(f'请输入文件地址:')
#另一种:直接读取文件地址
#excel_path = '../data/数据统计表.xlsx'

#读入
data = pd.read_excel(excel_path)
#定义一个names列表,可以统计多个负责人
names = {
    
    
	'陈文''需要发送的邮箱地址',
	'王杰''需要发送的邮箱地址'
	}
#文件夹名字
dirname = 'exceldir'
#如果没有文件夹,则自动创建
if not os.path.exists(dirname):
	os.makedirs(dirname)

#循环读取excel表中的数据
for name,email in names.items():
	#获取负责人的信息
	df = data.loc[data['负责人']==name]
	#保存路径及文件名
	filepath = os.path.join(dirname,f'{name}.xlsx')
	#写入数据
	writer = pd.ExcelWriter(filepath)
	#sheet1 是数据写到excel表的sheet1页
	df.to_excel(writer,'sheet1')
	#保存数据
	writer.save()
	if email:
		send_email(name,email,filepath)
	

3. The result of the operation:
>> 3.1 the generated folder
Insert picture description here

3.2 Generated file data
Insert picture description here

yagmail realizes automatic mail sending

Use yagmail to send emails.
Similarly, before using, you need to install the yagmail module

1. The cmd window uses pip to install

pip install yagmail

After the installation is complete, you can use it!

Let's write the code of
send_emial 2.send_email.py

# -*- coding:utf-8 -*-

"""
@ auth : carl_DJ
@ time : 2020-8-19
"""

import yagmail

#定义send_email方法
def send_email(name,send_to,filepath):
	#定义邮件发送的文本内容,以及附件
	contents = [
	f'{name},你好,数据统计信息已经整理完成,已发送,请查收!',
	filepath
	]
	#定义SMTP邮件的基本信息
	yag = yagmail.SMTP(
		user = '[email protected]',
		password = 'password',
		host = 'smtp.host'
	)
	yag.send(to = send_to,subject = '数据统计汇报',contents = contents)

3. The result is like this:
Insert picture description here

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/108092214