0

I'm currently creating a spreadsheet using xlwt and trying to export it out as an HttpResponse in django for a user to download. My code looks like this:

response = HttpResponse(mimetype = "application/vnd.ms-excel")
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'
work_book.save(response)
return response

Which seems to be the right way to do it, but I'm getting a:

Traceback (most recent call last):
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1233, in communicate
    req.respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 745, in respond
    self.server.gateway(self).respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1927, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\servers\basehttp.py", line 674, in __call__
    return self.application(environ, start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\handlers\wsgi.py", line 252, in __call__
    response = middleware_method(request, response)
  File "C:\dev\workspace-warranty\imcom\imcom\seo_mod\middleware.py", line 33, in process_response
    response.content = strip_spaces_between_tags(response.content.strip())
  File "C:\dev\workspace-warranty\3rdparty\django\utils\functional.py", line 259, in wrapper
    return func(*args, **kwargs)
  File "C:\dev\workspace-warranty\3rdparty\django\utils\html.py", line 89, in strip_spaces_between_tags
    return re.sub(r'>\s+<', '><', force_unicode(value))
  File "C:\dev\workspace-warranty\3rdparty\django\utils\encoding.py", line 88, in force_unicode
    raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte. You passed in 

(I left off the rest because I get a really long line of this \xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00 kind of stuff)

Do you guys have any ideas on something that could be wrong with this? Is is it because some of my write values look like this:

work_sheet.write(r,#,information) where information isn't cast to unicode?

RJones
  • 1
  • 1
  • Well...not all my code, but the part where the problem is. I can provide some more, but I'm not sure how the write statements or work book creation would cause the error. – RJones Jul 12 '11 at 20:57

2 Answers2

0
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'

should just be

response['Content-Disposition'] = 'attachment; filename = %s.xls' % u'Zinnia_Entries'

without quotes around .xls otherwise the output will be

u'attachment; filename = Zinnia_Entries +".xls"'

So try changing that.

But also check out this answer. It has a really helpful little function for outputing xls files.

django excel xlwt

Community
  • 1
  • 1
niklasdstrom
  • 742
  • 6
  • 17
  • I'll check out the first part tomorrow, and with regards to the link, I agree its helpful. Its actually where I got the idea to do what I'm doing here (As the function contents are largely the same as my code.) – RJones Jul 13 '11 at 00:03
  • The + thing wasn't the problem, but a good catch on your part. Thank you for the catch. Little things like that have been the bane of my existence before. – RJones Jul 13 '11 at 14:45
  • @RJones - And you removed the qoutes aswell? - perhaps then give us an example of what type of writes you try to do. You could also try making an as easy xls as possible (I'm thinking one field with just some text in it). Try if that works try adding some of that non-unicode data you're trying to write. See if you can bait out the reason of the error that way. – niklasdstrom Jul 13 '11 at 21:05
  • Yes sir. And I tried to make the xls simple, and in doing so ran into something I wasn't expecting. Even if I don't write anything, and just open the file and sheet, and then save, I get the same unicode error. `work_book = xlwt.Workbook() work_sheet = work_book.add_sheet('Blog Entries') response = HttpResponse(mimetype = "application/vnd.ms-excel") response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries' work_book.save(response) return response` Is my code now. What's strange is that the file saves fine if I save it onto the disk. – RJones Jul 13 '11 at 23:08
  • Sorry, code looks like this: `work_book = xlwt.Workbook()` `work_sheet = work_book.add_sheet('Blog Entries')` `response = HttpResponse(mimetype = "application/vnd.ms-excel")` `response['Content-Disposition'] = 'attachment; filename = %s.xls' % u'Zinnia_Entries'` `work_book.save(response)` `return response` – RJones Jul 13 '11 at 23:16
0

Solved the problem. Apparently someone had put some funky middleware stuff in that was hacking apart and appending and adding, ect. ect. to the file. When it shouldn't.

Anyway, with it gone the file exports perfectly.

@Storm - Thank you for all the help!

RJones
  • 1
  • 1