如何使用Openpyxl将EXCEL作为电子邮件附件发送而无需保存[即时] [英] How to send excel as email attachment using openpyxl without saving [On The Fly]

查看:14
本文介绍了如何使用Openpyxl将EXCEL作为电子邮件附件发送而无需保存[即时]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Openpyxl将EXCEL作为电子邮件附件发送,但没有在Django中[动态]保存

推荐答案

您可以将工作簿对象保存到BytesIO实例(从io导入BytesIO)

output = BytesIO()
workbook.save(output)

然后可以使用Django EmailMessage类创建电子邮件,并将BytesIO对象作为第二个参数作为文件附加。

email = EmailMessage(
    'Hello',
    'Body goes here',
    'from@example.com',
    ['to1@example.com', 'to2@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},
)

email.attach('file.xlsx', output.getvalue() , 'application/vnd.ms-excel')

请复习下次如何提问:)

这篇关于如何使用Openpyxl将EXCEL作为电子邮件附件发送而无需保存[即时]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆