Python自動化辦公小程序:實現(xiàn)報表自動化和自動發(fā)送到目的郵箱
↑ 關注 + 星標 ,每天學Python新技能
后臺回復【大禮包】送你Python自學大禮包
項目背景
一、報表自動化目的
1.節(jié)省時間,提高效率
2.減少錯誤
二、報表自動化范圍
1.頻率
2.開發(fā)時間
3.流程
三、實現(xiàn)步驟
總體概覽指標
反映某一數(shù)據(jù)指標的整體大小對比性指標
環(huán)比
相鄰時間段內(nèi)的指標直接作差同比
相鄰時間段內(nèi)某一共同時間點上指標的對比集中趨勢指標
中位數(shù)
眾數(shù)
平均數(shù)/加權平均數(shù)
離散程度指標
標準差
方差
四分位數(shù)
全距(極差)
最大界減最小界相關性指標
r
第一步:讀取數(shù)據(jù)源文件
import pandas as pdimport jsonimport pymysqlfrom sqlalchemy import create_engine# 打開數(shù)據(jù)庫連接conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='xxxx',charset = 'utf8')engine=create_engine('mysql+pymysql://root:xxxx@localhost/mysql?charset=utf8')def read_excel(file):df_excel=pd.read_excel(file)return df_exceldef read_json(file):with open(file,'r')as json_f:df_json=pd.read_json(json_f)return df_jsondef read_sql(table):sql_cmd ='SELECT * FROM %s'%tabledf_sql=pd.read_sql(sql_cmd,engine)return df_sqldef read_csv(file):df_csv=pd.read_csv(file)return df_csv
第二步:DataFrame計算
df=df[df['city_num'].notna()]df=df.drop(df[df['city_num']=='error'].index)df = df.city_num.value_counts()
def pie_chart(df):#將城市空值的一行刪除df=df[df['city_num'].notna()]#刪除errordf=df.drop(df[df['city_num']=='error'].index)#統(tǒng)計df = df.city_num.value_counts()df.head(10).plot.pie(subplots=True,figsize=(5, 6),autopct='%.2f%%',radius = 1.2,startangle = 250,legend=False)pie_chart(read_csv('user_info.csv'))
plt.savefig('fig_cat.png')pie = Pie()pie.add("",words)pie.set_global_opts(title_opts=opts.TitleOpts(title="前十地區(qū)"))#pie.set_series_opts(label_opts=opts.LabelOpts(user_df))pie.render_notebook()
def echart_pie(user_df):user_df=user_df[user_df['city_num'].notna()]user_df=user_df.drop(user_df[user_df['city_num']=='error'].index)user_df = user_df.city_num.value_counts()name=user_df.head(10).index.tolist()value=user_df.head(10).values.tolist()words=list(zip(list(name),list(value)))pie = Pie()pie.add("",words)pie.set_global_opts(title_opts=opts.TitleOpts(title="前十地區(qū)"))return pie.render_notebook()user_df=read_csv('user_info.csv')echart_pie(user_df)
可以進行保存,可惜不是動圖:
from snapshot_selenium import snapshotmake_snapshot(snapshot,echart_pie(user_df).render(),"test.png")
保存為網(wǎng)頁的形式就可以自動加載JS進行渲染了:
echart_pie(user_df).render('problem.html')os.system('problem.html')
第三步:自動發(fā)送郵件
import smtplibfrom email import encodersfrom email.header import Headerfrom email.utils import parseaddr,formataddrfrom email.mime.application import MIMEApplicationfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMEText#發(fā)件人郵箱asender="[email protected]"#收件人郵箱areceiver="[email protected]"#抄送人郵箱acc="[email protected]"#郵箱主題asubject="謝謝關注"#發(fā)件人地址from_addr="[email protected]"#郵箱授權碼password="####"#郵件設置msg=MIMEMultipart()msg['Subject']=asubjectmsg['to']=areceivermsg['Cc']=accmsg['from']="fanstuck"#郵件正文body="你好,歡迎關注fanstuck,您的關注就是我繼續(xù)創(chuàng)作的動力!"msg.attach(MIMEText(body,'plain','utf-8'))#添加附件htmlFile = 'C:/Users/10799/problem.html'html = MIMEApplication(open(htmlFile , 'rb').read())html.add_header('Content-Disposition', 'attachment', filename='html')msg.attach(html)#設置郵箱服務器地址和接口smtp_server="smtp.163.com"server = smtplib.SMTP(smtp_server,25)server.set_debuglevel(1)#登錄郵箱server.login(from_addr,password)#發(fā)生郵箱server.sendmail(from_addr,areceiver.split(',')+acc.split(','),msg.as_string())#斷開服務器連接server.quit()
評論
圖片
表情












