全文共3262字,預計學習時長10分鐘
「每天半小時學習編程,0基礎入門。」
「會Python的人,工作都不會太差。追上同齡人,就現在!」
在「全民Python」熱浪擊打中,作為一名非工程師,我也加入了「Python大軍」之中。
第一次使用python時,我感覺它不過是是excel的升級版,是一個可以改善數據分析工作的工具。
但隨著對python掌握得越好,我開始意識到它不僅僅是一個用來創造驚人的視覺效果或者執行高等數學函數的工具。
我開始探索python更多的可能性,並意識到我只是用到了這門語言的皮毛而已,而實際上我可以開始用它來自動化一些流程。
然而,我以前總是在擔心——「我不是一名軟體工程師,我辦不到」,這種想法一直讓我不敢把自動化流程付諸實踐。現在我仍不是一名軟體工程師,但我已經可以使一些流程自動化,使分析更有效,並成為了團隊中更有價值的成員。
從哪裡開始,心中始終有一個堅定的最終目標
為了有個正確的開端,心中需要有一個最終目標。
以我為例:當認識到我沒有充分挖掘這門程式語言的潛力時,我立即在想怎樣才能有效地把這些概念融入到我的工作中,從而更有效率。我靈光一現——報告。
作為分析員,公司經常要求我分析並製作報告。而作為其中優秀的一員,我把數據管道設置在一個計時器上,以便能夠自主輸入數據,然後寫好腳本並進行設置,就某一主題去創建表格、圖形和所需的統計信息。然而這個報告還未完成,我還需要去把這些視覺資料粘貼到演示文稿中,再把這封郵件發送到對方手中。
完美,我已經發現了哪裡可以自動化來提高工作效率。最終目標是:使用python,創建一個pdf格式的報告,將其作為附件添加到郵件並發送。
需要什麼
依賴項
下面是一些用於郵件自動發送的庫和軟體包。
#to create pdf report
from io import BytesIO
from reportlab.pdfgen import canvas
from django.http import HttpResponse#to automate email
import email, smtplib, sslfrom email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
以上是我想在個人項目中使用的,您可能不是全都需要。
報告
本文將列出一個簡單的例子。使用「canvas」創建pdf,並自定義一些設置,如背景色、字體和字體大小、文本字符串和圖像(或圖表)。
代碼
#name and create pdf
c = canvas.Canvas('automate_report.pdf')
#set background color (this color is yellow I do not recommend)
c.setFillColorRGB(1,1,0)#Select font and font size
c.setFont('Helvetica', 30)#create two text strings and set there position onthe pagec.drawString(5, 660,'Missing data chart for Titanic Data')
c.drawString(5, 630, 'Report generated by Python')#add an image determine it'sposition and width and height
c.drawImage('Survival.png', 5,90,480,400)#show page and save it
c.showPage()
c.save()
這時pdf創建好了。
需要注意以下事項:
1、 圖像與python腳本要位於同一目錄下。
2、 這只是一個基本大綱,如果想創建更複雜的pdf報告,建議訪問canvas官方文檔。
電子郵件
我們已經製作了一個pdf報告,現在看看怎麼通過網絡發送它!
在深入研究代碼之前,你可能需要創建一個接收該pdf的電子郵件帳戶,以便在自己的郵箱中檢測郵件,而不會用到朋友的郵箱帳戶做該實驗。
提示:確保不要忘記密碼。
代碼:
# assign key emailaspects to variables for easier future editingsubject = "Weekly Report"
body = "This is an email with the desired report attached"
sender_email = "connerleavitt@gmail.com"
receiver_email = "connerleavitt@icloud.com"
file = "automate_report.pdf" # in the same directory as script
password = "abc123"# Create the email head (sender, receiver, andsubject)email = MIMEMultipart()
email["From"] = sender_email
email["To"] = receiver_email
email["Subject"] = subject# Add body and attachment toemailemail.attach(MIMEText(body, "plain"))attach_file = open(file,"rb") # open the file
report = MIMEBase("application", "octate-stream")
report.set_payload((attach_file).read())
encoders.encode_base64(report)
#add report header with the file name
report.add_header("Content-Decomposition", "attachment",filename = file)
email.attach(report)#Create SMTP session for sending the mailsession =smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_email, password) #login with mail_id and password
text = email.as_string()
session.sendmail(sender_email, receiver_email, text)
session.quit()
print('Mail Sent')
代碼中我儘可能地多做注釋,這些注釋應該能幫你理解所有代碼是如何工作和相互作用的。作為個人偏好,我喜歡把很多硬代碼作為變量放到腳本裡,以便隨時編輯。
結論
任何事都可能用python來完成,現存的資源可以幫助你完成從前做不到的事。
Python真的是一個很棒的工具,幫助你更多產、更高效。
就算你和我一樣不是一名技術工程師,你也可以學習一下Python,利用Python,為你的工作帶來便利。
留言點讚關注
我們一起分享AI學習與發展的乾貨
如轉載,請後臺留言,遵守轉載規範