python 多sheet页写入excel并发邮件
2017-02-27 14:05
966 查看
#!/usr/bin/python # -*- coding: UTF-8 -*- import datetime import MySQLdb import os os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' #os.system('source /etc/profile') import sys import xlwt import xlrd import smtplib from time import strftime, localtime from datetime import timedelta, date import calendar from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header #sys.path.append('/usr/lib64/python2.6/site-packages/cx_Oracle.so') # 打开数据库连接 mydb = MySQLdb.connect("10.18.141.52","dba","xxxxxxxxxx","ys" ) print "mydb" #exit(1) # 使用cursor()方法获取操作游标 mycursor = mydb.cursor() mycursor.execute('SET CHARACTER SET utf8;') mycursor.execute('SET NAMES utf8;') mycursor.execute('SET character_set_connection=utf8;') sql1 = " CALL ys.pr_get_increament_report_by_plat_class1();" columnName1 = ['平台','动漫增量(部)','电影增量(部)','电视剧增量(部)','综艺增量(部)','增量(部)sum'] #定义所有的列名,共6列 style1 = xlwt.XFStyle() #设置单元格格式 style1.num_format_str = 'yyyy/m/d h:mm:ss' font = xlwt.Font() font.name = 'Times New Roman' font.bold = True font.italic = True font.height = 0x00C8 style1.font = font pattern = xlwt.Pattern() # Create the Pattern pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12 pattern.pattern_fore_colour = 5 style1.pattern = pattern # Add Pattern to Style wb=xlwt.Workbook(encoding='utf-8') #创建一个excel工作簿,编码utf-8,表格中支持中文 sheet1=wb.add_sheet('概览',cell_overwrite_ok=True) #创建一个sheet1 for c1 in range(len(columnName1)): #将列名插入表格,共6列 sheet1.write(0,c1,columnName1[c1]) try: mycursor.execute(sql1) results = mycursor.fetchall() rows = len(results) for i in range(rows): for j in range(6): print results[i][j] sheet1.write(i+1,j,results[i][j]) print '-- -- -- -- -- -- -- -- -- -- -- --' print 'ok-sql1' #wb.save('增量概览.xls') except Exception , e: print e mycursor.close() mycursor = mydb.cursor() mycursor.execute('SET CHARACTER SET utf8;') mycursor.execute('SET NAMES utf8;') mycursor.execute('SET character_set_connection=utf8;') sql2 = " CALL ys.pr_get_increament_report_by_plat_class2();" #sheet=wb.add_sheet('overView2') sheet2=wb.add_sheet('明细',cell_overwrite_ok=True) #创建一个sheet2 columnName2 = ['视频类型','平台','部数','集数','每部平均数'] for c2 in range(len(columnName2)): #将列名插入表格,共6列 sheet2.write(0,c2,columnName2[c2]) try: mycursor.execute(sql2) results2 = mycursor.fetchall() rows2 = len(results2) for ii in range(rows2): for jj in range(5): print results2[ii][jj] sheet2.write(ii+1,jj,results2[ii][jj]) #print jj print '== == == == == == == == == == == ==' print 'ok-sql2' ymdhms = strftime("%y%m%d%H%M%S",localtime()) wb.save('info数据增长' + ymdhms + '.xls') except Exception , e: print e mycursor.close() mydb.close() #########################以下发送邮件############################ mail_host="smtp.qq.com" #设置服务器 mail_user="305816339@qq.com" #用户名 mail_pass="3333333" #口令
#ckkbbzzbvfkwbgjisender = '305816339@qq.com'receivers = ['hongen@blice.cn'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱#创建一个带附件的实例message = MIMEMultipart()message['From'] = Header("py增长监控", 'utf-8')message['To'] = Header("数据增长", 'utf-8')subject = 'info数据增长'message['Subject'] =
Header(subject, 'utf-8')#邮件正文内容message.attach(MIMEText('亲爱的产品同学,数据增长详细请参见附件....','plain', 'utf-8'))# 构造附件1,传送当前目录下的 test.txt 文件att1 = MIMEText(open('info数据增长' + ymdhms + '.xls', 'rb').read(), 'base64', 'utf-8')att1["Content-Type"] = 'application/octet-stream'#
这里的filename可以任意写,写什么名字,邮件中显示什么名字att1["Content-Disposition"] = 'attachment; filename="info数据增长' + ymdhms + '.xls"'message.attach(att1)try: smtpObj = smtplib.SMTP_SSL() smtpObj.connect(mail_host, 465) # 25 为 SMTP 端口号 smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender,
receivers, message.as_string()) print "邮件发送成功 "except smtplib.SMTPException,e: print e
相关文章推荐
- 【整理】Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据
- 用Python将数据写入已存在Excel
- Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据
- 【详解】Python写入Excel数据表
- python 将中文转拼音后填充到url做参数并写入excel
- python2.6写看门狗并发邮件
- python:解析XML文件后写入EXCEL(一)
- python 在excel文件中写入date日期数据,以及读取excel日期数据,如何在python中正确显示date日期。
- PHP调用Python快速发送高并发邮件
- 提取zabbix监控平台单台服务器图形并发邮件python实现
- python 写入excel两种方法
- Python中生成(写入数据到)Excel文件
- python读取excel群发邮件(二)
- Python绘制excel表格到邮件正文区并发送脚本实例
- python 写入excel
- python连接oracle,把计算结果存入本地excel并发送带附件邮件
- selenium+python关于登录的脚本代码,使用了读取excel以及向excel中写入测试结果的方法
- Windows下利用Python动态检测外网IP并发邮件给邮箱
- python使用xlwt读取文件写入Excel
- 用python将excel数据写入mysql