Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PythonDocx创建文档,并通过django作为附件发送_Python_Django_Email_Email Attachments_Python Docx - Fatal编程技术网

使用PythonDocx创建文档,并通过django作为附件发送

使用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

我已使用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-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"]