溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用python生成oracle數(shù)據(jù)報(bào)表

發(fā)布時(shí)間:2020-06-23 06:43:41 來源:網(wǎng)絡(luò) 閱讀:3162 作者:p133 欄目:關(guān)系型數(shù)據(jù)庫
#!/usr/bin/env python
#coding:utf-8
# cx_Oracle 用于訪問oracle和導(dǎo)出數(shù)據(jù)
import cx_Oracle
# xlsxwriter 用于生成xlsx文件
import xlsxwriter
import time
import sys
# 導(dǎo)入郵件模塊
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
 
reload(sys)
sys.setsys.setdefaultencodingdefaultencoding("gbk")     #修改默認(rèn)編碼為“gbk”,解決中文編碼問題 不進(jìn)行設(shè)置會(huì)出現(xiàn) UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position 36: ordinal not in range(128)
 
con = cx_Oracle.connect("comm/12345678@orcl")
cursor = con.cursor()
 
#定義SQL腳本 由于腳本包含中文,使用decode('utf-8').encode('gbk') 對(duì)其進(jìn)行轉(zhuǎn)換
sql ='''
select count 收費(fèi)金額,
     locate 分中心
from business
'''.decode('utf-8').encode('gbk')
 
query1 = cursor.execute(sql)   #執(zhí)行查詢
 
title = [i[0] for i in query1.description]
 
date_now=time.strftime("%Y%m%d",time.localtime())
 
#文件名及其路徑
 
report_name='/excel/' + "業(yè)務(wù)數(shù)據(jù)".decode('utf-8').encode('gbk') + date_now + '.xlsx'
 
#生成xlsx格式oracle查詢統(tǒng)計(jì)報(bào)表
 
workbook = xlsxwriter.Workbook(report_name, {'constant_memory': True})
worksheet = workbook.add_worksheet()
print time.ctime()
data = cursor.fetchall()
print time.ctime()
worksheet.write_row(0, 0, title)
for row, row_date in enumerate(data):
    worksheet.write_row(row+1, 0, row_date)
print time.ctime()
cursor.close()
con.close()
workbook.close()
 
#以下代碼實(shí)現(xiàn)發(fā)送郵件
 
msg = MIMEMultipart()
 
#定義附件名
 
att1_name="業(yè)務(wù)數(shù)據(jù)".decode('utf-8').encode('gbk') + date_now + '.xlsx' 
 
#讀入附件,report_name
 
att1 = MIMEText(open(report_name, 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'p_w_upload; filename=%s' % att1_name.encode('gbk')
msg.attach(att1)
 
msg['to'] = 'boss@126.com'
msg['from'] = 'report@126.com'
msg['subject'] = "每周業(yè)務(wù)數(shù)據(jù)".decode('utf-8').encode('gbk')
try:
    server = smtplib.SMTP()
    server.connect('mail.126.com')
    server.login('report@126.com','12345678')#
    server.sendmail(msg['from'], msg['to'],msg.as_string())
    server.quit()
    print 'successful.'
except Exception, e:
    print str(e)


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI