在 outlook 中自动发送 matplotlib 图表 (python)

Automating the sending of a matplotlib chart in outlook (python)

我在 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='recipient@guyIwanttosendemailsto.com'
    
    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' 变量,电子邮件将完全按预期工作。我想知道你们中有没有人对如何完成这项任务有一些建议?

所以我可以通过将图表保存到我的桌面来解决问题:

plt.savefig('chart.png')

然后按如下方式更改 'MailInput' 行(用户是另一个动态变量):

MailInput='<html><body><img src="C:\Users\'+user+'\Desktop\chart.png" style="width:100%"/></p></body></html>'