Python 发送邮件 and 编辑Excel
记录一下Python 发送邮件的代码,这是半年前写的,不知道现在有什么类库的改动。
类库
- def SendEmail(self,SendEmailExcelUrl,ProjectName):
- sender ='发送人邮箱'
- senderPwd =os.environ.get('EmailPassword') #邮箱密码我这里是放在环境变量中了(Win)
- receivers ='123@123.com,123@123.com' #接收人邮箱
- #create a Instance
- message = MIMEMultipart()
- message['From'] = Header("发送人", 'utf-8')
- message['To'] = Header("邮件内容标题", 'utf-8')
- subject = '邮件标题’
- message['Subject'] = Header(subject, 'utf-8')
- #Message body content
- message.attach(MIMEText(' Dear All, \n\n ××× \n\n Regards, \n ××××××× ', 'plain', 'utf-8'))
- #Send xlsx file
- att = MIMEText(open(SendEmailExcelUrl, 'rb').read(), 'base64', 'utf-8')
- att["Content-Type"] = 'application/octet-stream'
- #Here you can rename the attachments in the message.
- att["Content-Disposition"] = 'attachment; filename="{}.xlsx"'.format(ProjectName)
- message.attach(att)
- try:
- smtpObj = smtplib.SMTP('代理服务器地址','代理服务器端口')
- smtpObj.starttls()
- smtpObj.login(sender, senderPwd)#代理服务器帐号密码验证
- smtpObj.sendmail(sender, receivers, message.as_string())
- #terminating the session
- smtpObj.quit()
- print("Send email successful")
- except smtplib.SMTPException as e:
- print(e.__doc__,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
- print(e.__context__,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
下面是编辑excel 仅仅用于记录
python编辑excel 还是比较坑的,个人观点。但是我是调用一个接口,该接口返回excel,该excel的样式什么的都有,如果我直接进行保存这样没问题,但是我需要对其加一列然后在保存excel就被破坏了,后来了解到目前该类库对样式的编辑支持的还不是很好
- def CreateCoreExcel(self,SendEmailExcelUrl,ApiSaveExcelSaveUrl):
- projectConfigJson=self.getProductfigJson()['Core']
- Group= projectConfigJson['Group']
- wb = load_workbook(ApiSaveExcelSaveUrl)
- ws = wb['Vulnerabilities']
- ws.insert_cols(2)
- ws.insert_cols(3)
- for index, row in enumerate(ws.rows):
- if index == 0:
- row[1].value='Group'
- row[2].value='ServiceName'
- else:
- values= row[22].value
- validationValues=True
- try:
- values.split('\\')
- except Exception as e:
- print(e.__doc__,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
- print(e.__context__,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
- validationValues=False
- print("values not is path values:{}, datetime:{} ".format(values,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
- if validationValues == True:
- strlist = values.split('\\')
- serviceName=strlist[3]
- groupName=""
- if serviceName in Group['1']:
- groupName="1"
- elif serviceName in Group['2']:
- groupName="2"
- elif serviceName in Group['3']:
- groupName="3"
- else:
- groupName="OtherGroup"
- row[1].value=groupName
- row[2].value=serviceName
- else :
- row[1].value="N/A"
- row[2].value="N/A"
- content = []
- index = 1
- for i in range(2,ws.max_row+2):
- contentJson ={}
- for j in range(1,ws.max_column+1):
- contentJson[ws.cell(index,j).value]=ws.cell(i,j).value
- content.append(contentJson)
- jsonstr=json.dumps(content)
- wbnew = Workbook()
- sheet = wbnew.active
- listHead=[]
- data= json.loads(jsonstr)
- for c,i in enumerate(data[0].keys()):
- sheet.cell(row=1,column=c+1,value=i)
- listHead.append(i)
- for r,i in enumerate(data):
- row=r+2
- for c,d in enumerate(listHead):
- sheet.cell(row=row,column=c+1,value=i.get(d,""))
- returnValue=os.path.exists(SendEmailExcelUrl)
- if returnValue==True:
- os.remove(SendEmailExcelUrl)
- wbnew.save(SendEmailExcelUrl)
该内容只是个人记录。
Python 发送邮件 and 编辑Excel的更多相关文章
- python接口自动化(三十二)--Python发送邮件(常见四种邮件内容)番外篇——上(详解)
简介 本篇文章与前边没有多大关联,就是对前边有关发邮件的总结和梳理.在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下, ...
- Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented
在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...
- Python自动化测试发送邮件太麻烦?!一起聊一聊 Python 发送邮件的3种方式
1. 前言 发送邮件,我们在平时工作中经用到,做为测试人员,在自动化测试中用的也比较多,需要发送邮件给某领导 SMTP是Python默认的邮件模块,可以发送纯文本.富文本.HTML 等格式的邮件 今天 ...
- python发送邮件
python发送邮件(无附件) ======================================================= #!/usr/bin/env python#coding ...
- python发送邮件及附件
今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题: 本人是mac如果没有按照依赖模块的请按照下面的截图安装 导入模块如 ...
- python 发送邮件实例
留言板回复作者邮件提醒 -----------2016-5-11 15:03:58-- source:python发送邮件实例
- 解读Python发送邮件
解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...
- python 发送邮件例子
想到用python发送邮件 主要是服务器 有时候会产生coredump文件 ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...
- VSTO学习笔记(五)批量编辑Excel 2010 x64
原文:VSTO学习笔记(五)批量编辑Excel 2010 x64 近期因为工作的需要,经常要批量处理大量的Excel文件,如果纯手工一个个修改,非常的麻烦,于是写了这么一个帮助类,希望能对你有所帮助. ...
随机推荐
- element admin中使用nprogress实现页面加载进度条
主要是知道是nprogress这个组件实现的就可以了,组件的使用方法可参考:https://blog.csdn.net/ltr15036900300/article/details/47321217 ...
- 2019-09-09 memcache
什么是缓存呢???缓存就是存贮数据(使用频繁的数据)的临时地方缓存可以认为是数据的大池子 一.数据缓存这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不 ...
- 大规模定制模式之于MES的三点思考
大规模定制(Mass Custermization) ,其目标是大规模生产定制化产品,并且在效率.质量(一致性)等指标方面与大规模批量生产等齐. 这是一种理想或者追求,其提出的背景是目前越发普遍的多品 ...
- 【转载】Gradle学习 第九章:Groovy快速入门
转载地址:http://ask.android-studio.org/?/article/17 To build a Groovy project, you use the Groovy plugin ...
- 【软件工程第二次作业】个人项目:WordCountPy
一.GitHub 地址 项目 GitHub 地址为:https://github.com/bytemo/WordCountTool 二.PSP表格 PSP2.1 Personal Software P ...
- 适用于Centos6/7,vsftp自动安装脚本
#!/bin/bash #vsftp install . /etc/rc.d/init.d/functions NUM=`rpm -q centos-release | awk -F '-' '{pr ...
- 记一次wsl上的pip3安装失败问题 The following packages were automatically installed and are no longer required:
转载请注明来源.https://www.cnblogs.com/sogeisetsu/.然后我的CSDNhttps://blog.csdn.net/suyues/article/details/103 ...
- Codeforces E. High Load(构造)
题目描述: High Load time limit per test 2 seconds memory limit per test 512 megabytes input standard inp ...
- BeyondCorps
This repository provides a short description of the BeyondCorp security model and resources for impl ...
- python移动目录下所有子目录文件到新的总目录
python移动目录下所有子目录文件到新的总目录 import os import shutil def file(p): p=p z=os.listdir(p) for i ...