xlwt Workbook.save using filelike object

Visto 974 veces
Saltar al primer mensaje no leído

David Gouldin

no leída,
30 oct 2008, 15:02:5830/10/08
a 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

no leída,
30 oct 2008, 16:50:0430/10/08
a 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

no leída,
30 oct 2008, 17:01:5630/10/08
a 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

no leída,
30 oct 2008, 17:48:5430/10/08
a 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

Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos