电子邮件xlsx附件Django def电子邮件页面(请求): t=get_模板('email.html') email=EmailMessage('Hello','Testbreak','email@hotmail.com',['email@hotmail.com']) email.content\u subtype=“html” 工作簿=xl.工作簿('ex.xlsx') 工作表=工作簿。添加工作表() 工作表。写入(0,0,‘总计’) 工作簿.关闭() email.attach(“ex.xlsx”,工作簿,“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”) email.send() 返回HttpResponse(t.render(Context({})),状态=200)

电子邮件xlsx附件Django def电子邮件页面(请求): t=get_模板('email.html') email=EmailMessage('Hello','Testbreak','email@hotmail.com',['email@hotmail.com']) email.content\u subtype=“html” 工作簿=xl.工作簿('ex.xlsx') 工作表=工作簿。添加工作表() 工作表。写入(0,0,‘总计’) 工作簿.关闭() email.attach(“ex.xlsx”,工作簿,“application/vnd.openxmlformats officedocument.spreadsheetml.sheet”) email.send() 返回HttpResponse(t.render(Context({})),状态=200),django,python-2.7,email-attachments,xlsxwriter,Django,Python 2.7,Email Attachments,Xlsxwriter,我在email.attach行尝试了以下更改: 工作簿.read()-read不是工作簿.getvalue()的属性 工作簿.getvalue()-getvalue不是工作簿的属性 工作簿-类型错误:“文件”对象不支持索引 def电子邮件页面(请求): t=get_模板('email.html') email=EmailMessage('Hello','Testbreak','email@hotmail.com',['email@hotmail.com']) email.content\u s

我在email.attach行尝试了以下更改:

  • 工作簿.read()-read不是工作簿.getvalue()的属性

  • 工作簿.getvalue()-getvalue不是工作簿的属性

  • 工作簿-类型错误:“文件”对象不支持索引

def电子邮件页面(请求):
t=get_模板('email.html')
email=EmailMessage('Hello','Test
break','email@hotmail.com',['email@hotmail.com']) email.content\u subtype=“html” f=StringIO.StringIO()#创建一个类似文件的对象 工作簿=xl。工作簿(f) 工作表=工作簿。添加工作表() 工作表。写入(0,0,‘总计’) 工作簿.关闭() email.attach(“b.xlsx”,f.getvalue(),'application/vnd.openxmlformats of icedocument.spreadsheetml.sheet') email.send() 返回HttpResponse(t.render(Context({})),状态=200)
这样,我们仍然可以使用xlsx writer来生成文件

def email_page(request):
    t = get_template('email.html')
    email = EmailMessage('Hello', 'Test<br>break', 'email@hotmail.com',['email@hotmail.com'])
    email.content_subtype = "html"

    workbook = xl.Workbook('ex.xlsx')
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, 'Total')
    workbook.close()
    email.attach("ex.xlsx", workbook, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    email.send()
    return HttpResponse(t.render(Context({})), status=200)
def email_page(request):
    t = get_template('email.html')
    email = EmailMessage('Hello', 'Test<br>break', 'email@hotmail.com',['email@hotmail.com'])
    email.content_subtype = "html"
    f = StringIO.StringIO() # create a file-like object 
    workbook = xl.Workbook(f)
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, 'Total')
    workbook.close()
    email.attach("b.xlsx", f.getvalue(), 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    email.send()
    return HttpResponse(t.render(Context({})), status=200)