创建Word文档,然后将其附加到电子邮件Django [英] Create word document and then attach it to email Django

查看:95
本文介绍了创建Word文档,然后将其附加到电子邮件Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用python_docx以便在Python中创建Word文档。我要实现的目标是,我需要在Django中创建文档文件,然后使用django.core.mail将其附加到电子邮件中,而不必将文件保存在服务器上。我尝试使用此方法创建Word文件(也从StackOverflow的答案中获取):

I'm currently using python_docx in order to create Word Documents in Python. What I'm trying to achieve is that I need to create Document File in Django and then attach it to an email using django.core.mail without having to save the file on the server. I've tried creating the Word File using this (taken from an answer also within StackOverflow):

def generate(self, title, content):
    document = Document()
    docx_title=title
    document.add_paragraph(content)

    f = BytesIO()
    document.save(f)
    length = f.tell()
    f.seek(0)
    response = HttpResponse(
        f.getvalue(),
        content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    )
    response['Content-Disposition'] = 'attachment; filename=' + docx_title
    response['Content-Length'] = length
    return response

然后在这里我尝试并尝试将响应附加到电子邮件:

And then here is where I experimented and tried to attach the response to the email:

def sendmail(self, name,email,description,location):
    message = EmailMessage('Custom Mail', 'Name: '+str(name)+'\nEmail: '+str(email)+'\nDescription: '+str(description)+'\nLocation: '+str(location), 'test@gmail.com',to=['testreceiver@gmail.com'])
    docattachment = generate('Test','CONTENT')
    message.attach(docattachment.name,docattachment.read(),docattachment.content_type)
    message.send()

我是否正在努力实现甚至可能?

Is what I'm trying to achieve even possible?

编辑:基于django.core.mail中attach()函数参数的message.attach()代码

I based the code of message.attach() from the parameters of the attach() function in django.core.mail

推荐答案

The问题出在此代码中:

The problem is in this code :

def sendmail(self, name,email,description,location):
    message = EmailMessage('Custom Mail', 'Name: '+str(name)+'\nEmail: '+str(email)+'\nDescription: '+str(description)+'\nLocation: '+str(location), 'test@gmail.com',to=['testreceiver@gmail.com'])
    docattachment = generate('Test','CONTENT')
    message.attach(docattachment.name,docattachment.read(),docattachment.content_type)
    message.send()

在此行中:

message.attach(docattachment.name,docattachment.read(),docattachment.content_type)

文档附加是从generate()功能获得的响应,并且 docattachment 没有任何名为的属性:name或read()

docattachment is the response got from generate() fucntion, and docattachment does not have any attribute named : name or read()

将上面的代码替换为:

message.attach("Test.doc",docattachment,'application/vnd.openxmlformats-officedocument.wordprocessingml.document')

创建文件时,它不应是HttpResponse,而应使用 BytesIO 来传递文件。

And the making of the file, it shouldn't be an HttpResponse and instead use BytesIO to deliver the file.

这篇关于创建Word文档,然后将其附加到电子邮件Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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