我在 matplotlib 中創建了一個圖表,我想自動發送它。
我已將圖表轉換為 png,然后將其另存為變數,如下所示:
chart = io.BytesIO()
plt.savefig(chart, format='png')
chart.seek(0)
im = Image.open(chart)
def get_plot():
return im
它不需要是 png 格式來完成任務,我只是認為以這種格式保存它是存盤它的最佳方式。
然后我設定了自動生成的電子郵件(今天是一個保存當天日期的變數):
def Emailer(text, subject, recipient):
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
mail.Display(True)
MailSubject= "Auto test mail at " today
MailInput=im
MailAdress='[email protected]'
Emailer(MailInput, MailSubject, MailAdress )
不幸的是,當我運行代碼時,我收到一條錯誤訊息:
TypeError: Objects of type 'PngImageFile' can not be converted to a COM VARIANT (but obtaining the buffer() of this object could)
如果我在“MailInput”中沒有“im”變數,則電子郵件將完全按預期作業。我想知道你們中是否有人對如何完成這項任務有一些建議?
uj5u.com熱心網友回復:
所以我能夠通過將圖表保存到我的桌面來解決這個問題:
plt.savefig('chart.png')
然后按如下方式更改“MailInput”行(用戶是另一個動態變數):
MailInput='<html><body><img src="C:\\Users\\' user '\\Desktop\\chart.png" style="width:100%"/></p></body></html>'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/353592.html
標籤:Python matplotlib 外表 蟒蛇成像库
