Python自動(dòng)化辦公小程序:實(shí)現(xiàn)報(bào)表自動(dòng)化和自動(dòng)發(fā)送到目的郵箱
↑ 關(guān)注 + 星標(biāo) ,每天學(xué)Python新技能
后臺(tái)回復(fù)【大禮包】送你Python自學(xué)大禮包
項(xiàng)目背景
一、報(bào)表自動(dòng)化目的
1.節(jié)省時(shí)間,提高效率
2.減少錯(cuò)誤
二、報(bào)表自動(dòng)化范圍
1.頻率
2.開(kāi)發(fā)時(shí)間
3.流程
三、實(shí)現(xiàn)步驟
總體概覽指標(biāo)
反映某一數(shù)據(jù)指標(biāo)的整體大小對(duì)比性指標(biāo)
環(huán)比
相鄰時(shí)間段內(nèi)的指標(biāo)直接作差同比
相鄰時(shí)間段內(nèi)某一共同時(shí)間點(diǎn)上指標(biāo)的對(duì)比集中趨勢(shì)指標(biāo)
中位數(shù)
眾數(shù)
平均數(shù)/加權(quán)平均數(shù)
離散程度指標(biāo)
標(biāo)準(zhǔn)差
方差
四分位數(shù)
全距(極差)
最大界減最小界相關(guān)性指標(biāo)
r
第一步:讀取數(shù)據(jù)源文件
import pandas as pdimport jsonimport pymysqlfrom sqlalchemy import create_engine# 打開(kāi)數(shù)據(jù)庫(kù)連接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計(jì)算
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)計(jì)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)
可以進(jìn)行保存,可惜不是動(dòng)圖:
from snapshot_selenium import snapshotmake_snapshot(snapshot,echart_pie(user_df).render(),"test.png")
保存為網(wǎng)頁(yè)的形式就可以自動(dòng)加載JS進(jìn)行渲染了:
echart_pie(user_df).render('problem.html')os.system('problem.html')
第三步:自動(dòng)發(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="謝謝關(guān)注"#發(fā)件人地址from_addr="[email protected]"#郵箱授權(quán)碼password="####"#郵件設(shè)置msg=MIMEMultipart()msg['Subject']=asubjectmsg['to']=areceivermsg['Cc']=accmsg['from']="fanstuck"#郵件正文body="你好,歡迎關(guān)注fanstuck,您的關(guān)注就是我繼續(xù)創(chuàng)作的動(dò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)#設(shè)置郵箱服務(wù)器地址和接口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())#斷開(kāi)服務(wù)器連接server.quit()
評(píng)論
圖片
表情












