使用PythonDocx创建文档,并通过django作为附件发送
我已使用docx创建了一个文档,并尝试将其作为电子邮件附件发送,但未将文档保存在服务器上。下面是我的代码:使用PythonDocx创建文档,并通过django作为附件发送,python,django,email,email-attachments,python-docx,Python,Django,Email,Email Attachments,Python Docx,我已使用docx创建了一个文档,并尝试将其作为电子邮件附件发送,但未将文档保存在服务器上。下面是我的代码: Document = document() paragraph = document.add_paragraph("Test Content") f = BytesIO() document.save(f) file_list = [] file_list.append(["Test.docx",f, "application/vnd.openxmlformats-officedocume
Document = document()
paragraph = document.add_paragraph("Test Content")
f = BytesIO()
document.save(f)
file_list = []
file_list.append(["Test.docx",f, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
email = EmailMessage(subject = 'Test', body = 'Hi', to = ['test@test.com'], attachments = file_list)
email.send()
我得到以下错误:
TypeError:应为类似对象的字节,而不是BytesIO
在线email.send()
如前所述,我尝试将BytesIO转换为StringIO
然后我得到一个错误:
TypeError:应为类似对象的字节,而不是StringIO
我从中查看了解决方案,但不理解文档如何作为附件发送
感谢您的帮助或指点
谢谢 答案在错误消息中
而不是
file_list.append(["Test.docx",f, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
是的
因为在我的代码中,f
是一个BytesIO
对象,并且f.getValue()
以字节的形式返回对象的内容
文件:
file_list.append(["Test.docx",f, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
file_list.append(["Test.docx", f.getValue(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]