发电子邮件
发电子邮件: smtp smtps
服务器: postfix sendmail qmail
客户端: web ,foxmail , outlook
收电子邮件: pop3 pip3s imap imaps
服务器: devocote
客户端: web ,foxmail , outlook
现生活 : 物体本身 --> 发信人发信人主题 --> 快递公司 --> 放到收信的人家里
邮件: 电子邮件 --> 发信人发信人主题 --> smpt服务器 --> 放到收信人邮箱里
收信人 --> 进入自己的邮箱查看,使用pop协议
python :
写信的工具: email
发信的工具: smtplib
因为大公司都对邮件作了安全限制,所以一般使用第三方工具无法使用帐号和密码登录。
同时如果希望第三方可以使用smtp pop 服务还要开启权限(smtp服务器,授权码)
示例1: 发送纯文本邮件
from email.mime.text import MIMEText
import smtplib
server = 'smtp.163.com'
user = '[email protected]'
to_user = user
passwd = '你的授权码'
subject = '沁园春·雪'
body = r'''北国风光,千里冰封,万里雪飘。
望长城内外,惟余莽莽;大河上下,顿失滔滔。
山舞银蛇,原驰蜡象,欲与天公试比高。
须晴日,看红装素裹,分外妖娆。
江山如此多娇,引无数英雄竞折腰。
惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。
一代天骄,成吉思汗,只识弯弓射大雕。
俱往矣,数风流人物,还看今朝。'''
msg = MIMEText(body)
msg['From'] = user
msg['To'] = to_user
msg['Subject'] = subject
tool = smtplib.SMTP()
tool.connect(server)
#tool.starttls()
tool.login(user,passwd)
tool.sendmail(user,to_user,msg.as_string())
tool.close()
示例2: 发送含图片和附件的邮件
import smtplib
# MIMEMultipart可以将多个MIME对象进行封装
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
server = 'smtp.163.com'
username = '[email protected]'
password = '授权码'
subject = r'张宗昌相比,古人的打油诗真是弱爆了888'
to_user = '[email protected]'
image_id = 'testimg'
image_path = 'baidu.png'
attach_path = 'abc.xlsx'
body = r' 这是一个测试:
详情见附件.'.format(image_id)
#添加图片函数
def addimg(src,imgid):
with open(src,'rb') as fp:
msgImage = MIMEImage(fp.read())
msgImage.add_header('Content-ID',imgid)
return msgImage
msg = MIMEMultipart('mixed')
msgtext = MIMEText(body,"html","utf-8")
msg.attach(msgtext)
msg.attach(addimg(image_path,image_id))
#创建附件
attachment = MIMEText(open(attach_path,'rb').read(),'base64','utf-8')
attachment["Content-Type"] = "application/octet-stream"
#指定邮件中附件的名字
attachment["Content-Disposition"] = 'attachment; filename="{}"'.format(attach_path)
msg.attach(attachment)
msg['Subject'] = subject
msg['From'] = username
msg['To'] = to_user
tool = smtplib.SMTP()
tool.connect(server)
tool.login(username, password)
tool.sendmail(username, to_user, msg.as_string())
tool.close()
生成excel文件
用途: 银行,支付宝等下载一年的消费情况
生成报表
如何生成excel文件
csv : 文件可以当成一种excel格式,不过是纯文本的
XlsxWriter: 这个支持功能强大
其它python的excel模块
安装:
pip3 install XlsxWriter
示例1:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet('abc')
worksheet.write('C2','name')
worksheet.write('D2','语文')
worksheet.write('E2','数学')
worksheet.write('F2','英文')
workbook.close()
示例2:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
#第一行
worksheet.write('C2','name')
worksheet.write('D2','语文')
worksheet.write('E2','数学')
worksheet.write('F2','英文')
#第二行
row2 = ['libai',100,90,80]
worksheet.write_row('C3',row2)
#第三行
row3 = ['quyuan',90,90,90]
worksheet.write_row(4-1,3-1,row3)
示例三:
import xlsxwriter
data = [
["name","语文","数学","英文","平均分"],
["libai",100,90,80,],
["quyuan",90,90,70,]
]
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format()
bold.set_bold()
bold_red = workbook.add_format()
bold_red.set_bold()
bold_red.set_font_color('red')
init = (12,8)
for i in range(1,len(data)):
tmp = data[i][1:]
avg = sum(tmp) / len(tmp)
avg = '{0:.1f}'.format(avg)
data[i].append(avg)
for j in range(len(data)):
row = init[0] + j
col = init[1]
if j == 0:
worksheet.write_row(row,col,data[j],bold_red)
else:
line = data[j]
for col_tmp in range(col,col+len(line)):
if col_tmp == col:
worksheet.write(row,col_tmp,line[col_tmp-col],bold)
else:
worksheet.write(row,col_tmp,line[col_tmp-col])
workbook.close()