1

am using xlwt module to create xls file with multiple sheets and i want to display Content disposition with the created file.

 wbk = xlwt.Workbook(encoding='utf-8')
 sheet = workbook.add_sheet('sheet1')

and then

 response.headers['Content-Type'] = \
    gluon.contenttype.contenttype('.xls')
response.headers['Content-disposition'] = 'attachment; filename=projects.xls'\

how can i make the content of wbk inside projects.xls?

Thanks in Advance

Neveen
  • 75
  • 3
  • 9

4 Answers4

2

Looking at the docs, it seems that you could write out to a StringIO, and then print that out.

import StringIO
output = StringIO.StringIO()
wbk.save(output)

and then use output.getvalue().

I have not tested this at all.

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
  • I suppose I should mention that, if using CPython, I'd probably use cStringIO. Whether or not you need it depends on your situation, though. – Xiong Chiamiov Aug 09 '10 at 11:05
2

[dis]claimer: I'm the maintainer of xlwt.

Workbook.save(destination) ...

if destination is an object with a write method (e.g. obtained by [c]StringIO.StringIO()):

xlwt writes to this but doesn't close it. What you use this for and how you dispose of the object are up to you.

else:

xlwt assumes that destination is a string which is interpreted as a file path; xlwt attempts to open a file, write to it, and close it.

John Machin
  • 81,303
  • 11
  • 141
  • 189
0

Use the response.body attribute to write the excel file:

wbk.save(response.body)
ars
  • 120,335
  • 23
  • 147
  • 134
0

in Django you would simply :

response = HttpResponse(my_data, mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=foo.xls'

wbk.save(response)
Dantalion
  • 323
  • 1
  • 2
  • 7