码迷,mamicode.com
首页 > 编程语言 > 详细

python

时间:2021-04-26 13:49:32      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ora   代理商   erro   receive   isp   登录   host   sage   multi   

# 每月将各分公司的业务情况汇总 再定时输出
import cx_Oracle
from openpyxl.styles import Font
import pandas as pd
import openpyxl
import time
from email.mime.multipart import MIMEMultipart
import os
from private_module import last_month,excel_format
from openpyxl import load_workbook

os.chdir(r"/uendata/ushks/nshks_data")

# 定义时间月份函数
def str_last_month():
    import datetime
    today=datetime.date.today()
    first_day=today.replace(day=1)
    before_month=first_day-datetime.timedelta(days=1)
    return before_month.strftime("%Y%m")
last_month=str_last_month()


# 调用存储
start=time.time()
# 数据库连接
conn=cx_Oracle.connect(‘bi_pq‘,‘qx1234‘,‘192.168.21.18:1521/ushks‘)
# 调用存储
cur=conn.cursor()
cur.callproc(‘HK_SHKS_BRANCH_TRADE‘)
conn.commit()
cur.close()

end=time.time()
print("存储过程执行成功,总时间为:",end-start)

# 数据库数据读取
sql_branch_trade=‘select * from HK_BRANCH_TRADE_MONTH‘
# 将sql文件进行读取
df_branch_trade=pd.read_sql(sql_branch_trade,conn)

filename=r"%s各分公司业务数据.xlsx"%last_month.str_last_month()

# 导出excel
header_list=[‘编号‘,‘机构名称‘,‘新增代理商数量‘,‘月份‘,‘商户数量‘,‘交易额‘,‘终端入库量‘,‘终端激活量‘,‘无押金一阶段达标数量‘,‘有押金激活数量‘]
df_branch_trade.to_excel(filename,index=False,header=header_list) 

#对excel进行格式更改
excel_format.excel_format(filename)

workbook_1=load_workbook(filename)

sheet_1=workbook_1.active

sheet_1.column_dimensions[‘B‘].width = 40
sheet_1.column_dimensions[‘F‘].width = 15


# sheet1.row_dimensions[2].height = 15
workbook_1.save(filename)


import smtplib
from email.header import make_header
from email.mime.text import MIMEText
mail_host = ‘smtp.exmail.qq.com‘  
# mail_user = ‘peng@163.com‘  
# mail_pass = ‘mrgpw5Z7j‘   

mail_user = ‘dbagh2@163.com‘  
mail_pass = ‘Paklhjyns1hj23‘   
sender = ‘dhjba2@163.com‘  
to_reciver = [‘zm@163.com‘,‘ba2@163.com‘]
cc_reciver = [‘zb1@163.com‘,‘peng@163.com‘]

receivers = to_reciver + cc_reciver

message = MIMEMultipart()
message[‘From‘] = sender
message[‘To‘] = ‘;‘.join(receivers)
message[‘Cc‘] = ‘;‘.join(cc_reciver)
message[‘Subject‘] = ‘分公司按月汇总数据‘
message_txt = MIMEText(‘你好,\n 附件数据为各分公司业务数据,请注意查收,有问题烦再请及时告知。‘,‘plain‘,‘utf-8‘) 

#添加excel附件一
message_xlsx = MIMEText(open(filename, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
message_xlsx.add_header(‘Content-Disposition‘, ‘attachment‘, fileName = "%s" % make_header([(filename, ‘UTF-8‘)]).encode(‘UTF-8‘) ) 
message.attach(message_xlsx)# 添加excel文件到邮件-附件中去

#添加excel附件二
message_xlsx_2 = MIMEText(open(filename_sx, ‘rb‘).read(), ‘base64‘, ‘utf-8‘)
message_xlsx_2.add_header(‘Content-Disposition‘, ‘attachment‘, fileName = "%s" % make_header([(filename_sx, ‘UTF-8‘)]).encode(‘UTF-8‘) ) 
message.attach(message_xlsx_2)# 添加excel文件到邮件-附件中去

# 添加正文
message.attach(message_txt)

try:
    smtpObj = smtplib.SMTP() 
    #连接到服务器
    smtpObj.connect(mail_host,25)
    #登录到服务器
    smtpObj.login(mail_user,mail_pass) 
    #发送
    smtpObj.sendmail(
        sender,receivers,message.as_string()) 
    #退出
    smtpObj.quit() 
    print(‘success‘)
except smtplib.SMTPException as e:
    print(‘error‘,e) #打印错误
  

  

python

标签:ora   代理商   erro   receive   isp   登录   host   sage   multi   

原文地址:https://www.cnblogs.com/cstark/p/14700326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!