gpt4 book ai didi

Python 如何在电子邮件正文中使用 DataFrame 输出作为文本

转载 作者:行者123 更新时间:2023-12-02 14:58:24 25 4
gpt4 key购买 nike

我有数据帧 Df 的输出,我可以将它作为附件发送到邮件中,但无法在邮件正文中打印 Df 值。请建议在电子邮件正文中打印我的 Df 值,这样我就不必添加附件了。

import win32com.client as win32

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'abc@xyg.com'
mail.Subject = 'Madsaage Subject'
mail.Body = 'print(Df)'
mail.HTMLBody = 'Please Find Attached' # This field is optional
# To attach a file to the email (optional):
mail.Attachments.Add('C:/XYZ/transport.csv')
mail.Send()

最佳答案

考虑 pandas 的 DataFrame.to_html()它将数据 fame 渲染到 HTML 表中,以便在电子邮件的 HTMLBody 中使用。如果您不指定文件名,它将以字符串形式输出表格。

...
mail.Subject = 'Madsaage Subject'

mail.HTMLBody = '''<h3>Please find data attached and below.</h3>
{}'''.format(Df.to_html())
...

或者,使用 DataFrame.to_string()在电子邮件的正文中。

mail.Body = '''Please find data attached and below.\n\n
{}'''.format(Df.to_string())

关于Python 如何在电子邮件正文中使用 DataFrame 输出作为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51799650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com