作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有数据帧 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/
我是一名优秀的程序员,十分优秀!