xlwt Workbook.save using filelike object

971 views
Skip to first unread message

David Gouldin

unread,
Oct 30, 2008, 3:02:58 PM10/30/08
to python-excel
I'm using xlwt with the gdata python client library to easily create
formatted spreadsheets for google docs. The documented behavior of
Workbook.save takes a filename as a parameter and saves the xls to the
filesystem. However, the implementation (using
CompoundDoc.XlsDoc.save) supports using either a filename or a
filelike object. Obviously for our purposes, the latter is preferable
as we're simply passing the object on to the gdata client library for
uploading to google docs.

Is this behavior I can depend on in the future or a happy coincidence?

Joseph Wilhelm

unread,
Oct 30, 2008, 4:50:04 PM10/30/08
to python...@googlegroups.com
Actually, xlwt will already accept a file-like object. This is just
something has not (yet) been documented. In fact, I have done exactly
that with a StringIO object in this snippet here:
http://www.djangosnippets.org/snippets/1151/

Enjoy!

--Joey

John Machin

unread,
Oct 30, 2008, 5:01:56 PM10/30/08
to python...@googlegroups.com
On 31/10/2008 06:02, David Gouldin wrote:
> I'm using xlwt with the gdata python client library to easily create
> formatted spreadsheets for google docs. The documented behavior of
> Workbook.save takes a filename as a parameter and saves the xls to the
> filesystem.

Does it? What I see (released version, see
http://pypi.python.org/pypi/xlwt) in ..../xlwt/doc/xlwt.html is:
"""
save(filename_or_stream) [#]

This method is used to save Workbook to a file in native Excel format.

filename_or_stream

This can be a string containing a filename of the file, in
which case the excel file is saved to disk using the name provided.

It can also be a stream object with a write method, such as a
StringIO, in which case the data for the excel file is written to the
stream.
"""

> However, the implementation (using
> CompoundDoc.XlsDoc.save) supports using either a filename or a
> filelike object. Obviously for our purposes, the latter is preferable
> as we're simply passing the object on to the gdata client library for
> uploading to google docs.
>
> Is this behavior I can depend on in the future or a happy coincidence?

Yes.

Cheers,
John

Chris Withers

unread,
Oct 30, 2008, 5:48:54 PM10/30/08
to python...@googlegroups.com
John Machin wrote:
> Does it? What I see (released version, see
> http://pypi.python.org/pypi/xlwt) in ..../xlwt/doc/xlwt.html is:
> """
> save(filename_or_stream) [#]
>
> This method is used to save Workbook to a file in native Excel format.

I think someone read the code and not the docs, that's gratitude for you ;-)

Chris

--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk

Reply all
Reply to author
Forward
0 new messages