使用python解决烦人的每周邮件汇总!

时间:2024-10-19 10:03:50

  最近开始接手BI工作,其中又一个繁琐又不得不做的事,就是每周五都得汇总上个财务周的数据给运营人员!

  作为一个懒人,只能把这件事交由电脑去处理了。

  初步的idea:周五11点前mac自动执行汇总程序->读取数据库数据->写入模版汇总excel->以邮件的形式发送给相关运营人员。

  为了实现此idea并不考虑性能的情况下,选择了实用python这种简单且代码量少的方式。

  首先先实现读取数据库数据部分

  1.python_constant.py(常量部分,肯定要屏蔽具体信息啦,不然被公司投诉了!)

 # -*- coding: utf-8 -*- 

 config_b2b = {
'user':'sqlbidev',
'password':'xxx',
'host':'xxx',
'database':'ODS_B2B'
} config_b2c = {
'user':'sqlbidev',
'password':'xxx',
'host':'xxx',
'database':'ODS_B2C'
} config_o2o = {
'user':'sqlbidev',
'password':'xxx',
'host':'xxx',
'database':'ODS_O2O'
} data_b2c = {
'东北区域':'C6',
'海南区域':'C7',
'华北区域':'C8',
'华东区域':'C9',
'华南区域':'C10',
'山东区域':'C11',
'西北区域':'C12',
'中南区域':'C13'
} data_o2o = {
'东北区域':'D6',
'海南区域':'D7',
'华北区域':'D8',
'华东区域':'D9',
'华南区域':'D10',
'山东区域':'D11',
'西北区域':'D12',
'中南区域':'D13'
} data_b2b = {
'东北区域':'E6',
'海南区域':'E7',
'华北区域':'E8',
'华东区域':'E9',
'华南区域':'E10',
'山东区域':'E11',
'西北区域':'E12',
'中南区域':'E13'
}

  2. python_query.py(查询数据,由于查三个库的数据,所以特定三个方法,简单实用!)

 # -*- coding: utf-8 -*-
import pymssql
import python_constant as constant #读取b2c的会员数据
def query_b2c(start_time, end_time):
print('开始查询b2c数据')
conn = pymssql.connect(**constant.config_b2c)
cursor = conn.cursor(as_dict=True) sql = "select ds.FirstLevelRegion, COUNT(distinct consignee_mobile) ActiveMem from ( \
select * from openquery(B2C,'SELECT ord.* FROM tbl_order ord, tbl_seller ts \
WHERE ord.seller_no = ts.id AND ord.basic_state <> 404 AND DATE_FORMAT(ord.create_time, ''%Y%m%d'') between ''{0}'' and ''{1}'' ') ) ord \
inner join Dim_Store ds on ord.seller_no = ds.StoreId group by ds.FirstLevelRegion".format(start_time, end_time)
cursor.execute(sql)
result = [row for row in cursor]
conn.close()
print('结束查询b2c数据')
return result #读取o2o的会员数据
def query_o2o(start_time, end_time):
print('开始查询o2o数据')
conn = pymssql.connect(**constant.config_o2o)
cursor = conn.cursor(as_dict=True) sql = "select ds.FirstLevelRegion, COUNT(distinct buyer_phone) ActiveMem from ( \
select * from openquery(O2O, 'SELECT tod.* FROM tbl_order tod,tbl_outlet_basic_info tobi WHERE 1 = 1 AND tod.outlet_id = tobi.outlet_id \
AND DATE_FORMAT(tod.create_time, ''%Y%m%d'') between ''{0}'' and ''{1}'' ') ) ord \
inner join Dim_Store ds on ord.outlet_id = ds.StoreId group by ds.FirstLevelRegion".format(start_time, end_time)
cursor.execute(sql)
result = [row for row in cursor]
conn.close()
print('结束查询o2o数据')
return result #读取b2b的会员数据
def query_b2b(start_time, end_time):
print('开始查询b2b数据') conn = pymssql.connect(**constant.config_b2b)
cursor = conn.cursor(as_dict=True) sql = "select ds.FirstLevelRegion, COUNT(distinct contact_mobile) ActiveMem from ( \
select * from openquery(B2B, 'SELECT * FROM order_info tod\
where DATE_FORMAT(tod.create_time, ''%Y%m%d'') between ''{0}'' and ''{1}'' ') ) ord \
inner join Dim_Store ds on ord.supply_user_code = ds.StoreId \
group by ds.FirstLevelRegion".format(start_time, end_time)
cursor.execute(sql)
result = [row for row in cursor]
conn.close()
print('结束查询b2b数据')
return result

  3. 实现发送email邮件

 #!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header sender = 'll-chen6@hnair.com'
receiver = 'chenqianyun@ccoop.com.cn'
subject = '每周会员统计数'
smtpserver = 'smtp.hnair.com'
username = 'xxxx'
password = 'xxxx' def send(path):
msg = MIMEText('<html><h1>倩云,你好!汇总数据请查收附件</h1></html>','html','utf-8') msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = Header(subject, 'utf-8') title = '每周会员统计数.xlsx'.decode('utf-8') #构造附件
att = MIMEText(open(path, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename=%s' %title.encode('gb2312') msgRoot.attach(msg)
msgRoot.attach(att) smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

  5. 主体调用

 # -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
import python_query as query
import python_email as email
import time
import datetime
import python_constant as constant #date_time = datetime.datetime.now()
date_time = datetime.datetime(2017, 2, 28)
dayOfWeek = date_time.weekday()
#假如是星期五则执行查询
if dayOfWeek == 4:
start_date = date_time + datetime.timedelta(-27)
end_time = date_time.strftime("%Y%m%d")
start_time = start_date.strftime("%Y%m%d")
print "开始时间:{0}, 结束时间:{1}".format(start_time, end_time)
#获取当前月份
month = date_time.month
#获取统计周
query_day = start_date.strftime("%m.%d") + '-' + date_time.strftime("%m.%d")
wb = load_workbook('/Users/chenlili/Desktop/BI立项/汇报数据模版/第3周活跃会员数.xlsx')
sheet = wb.get_sheet_by_name(u'Sheet1')
sheet['B2'] = str(month) + '月'
sheet['B4'] = query_day #填充b2c数据
b2c_result = query.query_b2c(start_time, end_time)
if b2c_result:
for data in b2c_result:
first_level_region = data['FirstLevelRegion'].encode('utf-8')
row = constant.data_b2c[first_level_region]
print first_level_region + ', ' + str(data['ActiveMem'])
if row:
sheet[row] = data['ActiveMem']
#填充o2o数据
o2o_result = query.query_o2o(start_time, end_time)
if o2o_result:
for data in o2o_result:
first_level_region = data['FirstLevelRegion'].encode('utf-8')
row = constant.data_o2o[first_level_region]
print first_level_region + ', ' + str(data['ActiveMem'])
if row:
sheet[row] = data['ActiveMem'] #填充b2b数据
try:
b2b_result = query.query_b2b(start_time, end_time)
if b2b_result:
for data in b2b_result:
first_level_region = data['FirstLevelRegion'].encode('utf-8')
row = constant.data_b2b[first_level_region]
print first_level_region + ', ' + str(data['ActiveMem'])
if row:
sheet[row] = data['ActiveMem']
except Exception, e:
print '异常错误: ' + str(e)
else:
pass
finally:
print '解除异常继续执行' path = '/Users/chenlili/Desktop/BI立项/汇报数据模版/第3周活跃会员数副本.xlsx'
wb.save(path) #发送邮件
email.send(path)
else:
print "未到星期五!"

  完结,提供github上的代码:friday_task,暂时未实现自动执行脚本的shell,还在想怎么实现。