I have created a chart in matplotlib which I would like to send somewhat automatically.
I've converted the chart to png and then saved it as a variable as follows:
chart = io.BytesIO()
plt.savefig(chart, format='png')
chart.seek(0)
im = Image.open(chart)
def get_plot():
return im
It doesn't need to be in a png format to complete the task, I just thought saving it in that format would be the best way to store it.
I have then set up auto-generated email (today is a variable that holds the day's date):
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 )
Unfortunately when I run the code, I get an error message saying:
TypeError: Objects of type 'PngImageFile' can not be converted to a COM VARIANT (but obtaining the buffer() of this object could)
If I didn't have the 'im' variable in the 'MailInput' the email works exactly as intended. I was wondering if any of you had some advice on how to finish this task?
CodePudding user response:
So I was able to fix the issue by saving the chart to my desktop:
plt.savefig('chart.png')
Then changing the 'MailInput' line as follows (user being another dynamic variable):
MailInput='<html><body><img src="C:\\Users\\' user '\\Desktop\\chart.png" style="width:100%"/></p></body></html>'