Create file on the fly for downloadling

57 views
Skip to first unread message

陶艺夫

unread,
Jul 18, 2009, 9:32:47 PM7/18/09
to web...@googlegroups.com
Hi, all friends.
I'm using PyExelerator to create Excel report for users to download. I tried to save the Workbook object to a StringIO which is said a file-like object,  but the result is an empty file. Following are the codes:

def get_report():
    #.... generating workbook codes
    f = StringIO.StringIO()
    doc = CompoundDoc.XlsDoc()
    doc.save_to(f,w.get_biff_data())
    #the "doc.save_to" func was added to the Workbook class in the pyExelerator's module by myself. There was a original
    #"save" method there take two argument, the first one is a file object, the second one is the same as mine.
    response.headers['Content-type']='application/vnd.ms-excel'
    response.headers['Content-Disposition']='attachment; filename=test.xls'
    response.headers['Content-Title']='test.xls'
    response.headers['Content-Length'] = f.tell()
    f.seek(0)
    return f.read()

What's the problem? Thanks in advance.


mdipierro

unread,
Jul 18, 2009, 9:56:30 PM7/18/09
to web2py-users
Try replace

f.seek(0)
return f.read()

with

return f.getvalue()

if it does not work than it is a PyExelerator issue

陶艺夫

unread,
Jul 18, 2009, 11:36:38 PM7/18/09
to web...@googlegroups.com
Thank you for replying.
It still does't work. In pyExcelerator, the file object opened to write to is a binary file(opened with "wb" attribute). I don't know whether the StringIO can handle its content as binary data. If it do, how? Or is there something other than StringIO to do so in Python?


2009/7/19 mdipierro <mdip...@cs.depaul.edu>

Yarko Tymciurak

unread,
Jul 19, 2009, 12:27:17 AM7/19/09
to web...@googlegroups.com
Have you looked at xlwt http://www.python-excel.org/ --- https://secure.simplistix.co.uk/svn/xlwt/trunk ?

It claims to have fixed bugs w/ PyExelerator; see https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/doc/xlwt.html

the Workbook.save() function says it will save to a stream, such as StringIO....

in fact, they seem to have an active google group, and there is an example of saving to StringIO:

http://groups.google.com/group/python-excel/browse_thread/thread/581a01953ce3575f/61e40855d94c1eb3?lnk=gst&q=save%28%29#61e40855d94c1eb3

http://www.djangosnippets.org/snippets/1151/   (look for 'output = StrintIO.StringIO()' and 'book.save(output)')

Hope that's helpful...



2009/7/18 陶艺夫 <artm...@gmail.com>

陶艺夫

unread,
Jul 19, 2009, 2:03:53 AM7/19/09
to web...@googlegroups.com
Thank Yarko!
It worked when I changed to use xlwt. :)
I love this group for it's so helpful.

2009/7/19 Yarko Tymciurak <yar...@gmail.com>

Yarko Tymciurak

unread,
Jul 19, 2009, 2:24:14 AM7/19/09
to web...@googlegroups.com
Glad to hear it worked for you...
I saw they were active, had a presentation at Europython  just last month...

2009/7/19 陶艺夫 <artm...@gmail.com>
Reply all
Reply to author
Forward
0 new messages