用Python pandas查询sqlserver数据库保存至excel邮件发送

import pymssql

import xlwt

import datetime

import pandas as pd

import numpy as np

import xlsxwriter

#自己写的发送邮件py

import SendMail

def get_sel_excel():

    #建立数据库连接

    conn = pymssql.connect('10.0.1.2', 'sa', 'abcdefg', 'stu')

    #获取当前日期

    today=datetime.date.today()

    #保存路径

  save_file_path='D:\\PySendMailrpot\\'+str(today)+'商品信息变更.xlsx'

    #数据库查询语句,此处我调用存储过程

    sql1="proc_get_changestate;"

    sql2="proc_get_changeSale;"

    sql3="proc_get_changeNew;"

    sql4="proc_get_ProSale;"

    #pandas 执行sql语句保存至dataframe

    df1=pd.read_sql(sql1,conn)

    df2=pd.read_sql(sql2,conn)

    df3=pd.read_sql(sql3,conn)

    df4=pd.read_sql(sql4,conn)

    #pd保存至excel文件

    writer = pd.ExcelWriter(save_file_path)

    workbook = writer.book

    #自定义表格样式

    fmt = workbook.add_format({"font_name": u"微软雅黑"})

    percent_fmt = workbook.add_format({'num_format': '0.00%'})

    amt_fmt = workbook.add_format({'num_format': '#,##0'})

    border_format = workbook.add_format({'border': 1})

    note_fmt = workbook.add_format(

        {'bold': True, 'font_name': u'微软雅黑','font_size': 14, 'font_color': 'red', 'align': 'center', 'valign': 'vcenter'})

    date_fmt = workbook.add_format({'bold': False, 'font_name': u'微软雅黑', 'num_format': 'yyyy-mm-dd HH:SS'})

    amt_fmt1 = workbook.add_format(

        {'bold': True, 'font_size': 10, 'font_name': u'微软雅黑','num_format': '#,##0', 'bg_color': '#9FC3D1',

        'valign': 'vcenter', 'align': 'center'})

    highlight_fmt = workbook.add_format({'bg_color': '#FFD7E2', 'num_format': '0.00%'})

    #df写入表格第一个页签

    df1.to_excel(writer, sheet_name=u'状态变更', encoding='utf8', header=False, index=False, startcol=0, startrow=1)

    worksheet1 = writer.sheets[u'状态变更']

    #对第B列至I列表格样式设置

    worksheet1.set_column('B:I',9, fmt)

    worksheet1.set_column('A:A',15, fmt)

    worksheet1.set_column('C:C',25, fmt)

    #对第0行标题行样式设置

    for col_num, value in enumerate(df1.columns.values):

        print('col_num:'+str(col_num)+'value:'+str(value))

        worksheet1.write(0, col_num, value, amt_fmt1)

    #df写入表格第二个页签  

    df2.to_excel(writer, sheet_name=u'价格变更', encoding='utf8', header=False, index=False, startcol=0, startrow=1)

    worksheet2 = writer.sheets[u'价格变更']

    worksheet2.set_column('B:I',9, fmt)

    worksheet2.set_column('A:A',25, fmt)

    worksheet2.set_column('D:D',25, fmt)

    for col_num, value in enumerate(df2.columns.values):

        if col_num==0:

            worksheet2.write(0, col_num, value, date_fmt)

        else:

            worksheet2.write(0, col_num, value, amt_fmt1)

    df3.to_excel(writer, sheet_name=u'新品', encoding='utf8', header=False, index=False, startcol=0, startrow=1)

    worksheet3 = writer.sheets[u'新品']

    worksheet3.set_column('B:I',9, fmt)

    worksheet3.set_column('A:A',15, fmt)

    worksheet3.set_column('C:C',25, fmt)

    for col_num, value in enumerate(df3.columns.values):

        worksheet3.write(0, col_num, value, amt_fmt1)

    df4.to_excel(writer, sheet_name=u'促销', encoding='utf8', header=False, index=False, startcol=0, startrow=1)

    worksheet4 = writer.sheets[u'促销']

    worksheet4.set_column('B:I',9, fmt)

    worksheet4.set_column('A:A',15, fmt)

    worksheet4.set_column('C:C',25, fmt)

    worksheet4.set_column('H:H',25, fmt)

    for col_num, value in enumerate(df4.columns.values):

        worksheet4.write(0, col_num, value, amt_fmt1)

    # 保存

    writer.save()

    print("导出成功!")

sendMail(save_file_path)

# 调用sendMail邮件发送

def sendMail(save_file_path):

to_addr ='[email protected],[email protected]'  # 收件人

msg_subject ='商品信息变更'                        # 邮件标题

cc_addr ='[email protected],[email protected]'  # 抄送人

today=datetime.date.today()                      # 当前日期

file_path =save_file_path                            # 附件地址

file_name=str(today)+'商品信息变更.xlsx'    # 文件名称

print(file_name)

sendMail.send_email(to_addr,msg_subject,file_name,cc_addr,file_path)

if __name__ == "__main__":

    file_excel = r"D:\SendMail\repot\商品信息变更.xlsx"

    get_sel_excel()


用Python pandas查询sqlserver数据库保存至excel邮件发送_第1张图片
用Python pandas查询sqlserver数据库保存至excel邮件发送_第2张图片

你可能感兴趣的:(用Python pandas查询sqlserver数据库保存至excel邮件发送)