quick search:
 

Simple Excel Spreadsheet

Submitted by: kris
Last Edited: 2004-10-14

Category: Python(Script)

Average rating is: 0.0 out of 5 (0 ratings)

Description:
It's dead simple to output in Microsoft Excel using Python.

Source (Text):
request = container.REQUEST
RESPONSE =  request.RESPONSE

table_example = """<table>
     <tr>
          <td colspan="4" align="center"><h3>This is the Header for a Spreadsheet</h3></td>
     </tr>
     <tr bgcolor="#CCCCCC">
          <td align="center"><b>Data Column 1</b></td>
          <td align="center"><b>Data Column 2</b></td>
          <td align="center"><b>Data Column 3</b></td>
          <td align="center"><b>Data Column 4</b></td>
     </tr>
     <tr>
          <td align="center">Information</td>
          <td align="center">More info</td>
          <td align="center">...</td>
          <td align="center">&nbsp;</td>
     </tr>
     <tr>
          <td align="center"><font color="#CC0000">This colour is from a &lt;font&gt; tag</font></td>
          <td align="center"><span style="color:#CC0000;">This colour is from a &lt;span&gt; tag</span></td>
          <td align="center">...</td>
          <td align="center">&nbsp;</td>
     </tr>
     </table>"""

RESPONSE.setHeader("Content-type","application/vnd.ms-excel")
RESPONSE.setHeader("Content-disposition","attachment;filename=examplespreadsheet.xls")

return table_example

Explanation:
Just make sure you set your response headers, and you'll generate Excel without a hitch.
(Of course you need to have a well-formed html table to convert--but that's all!)

Check your target version of Excel; I don't know if all versions support CSS or not.
If not, you have the trusty font tag to help with basic formatting


Comments:

Problems with this implemenation by pspierce - 2004-10-14
I initially thought Excel converted the html upon openning the file then acted upon it as though it were in BIFF format (or a regular Excel file).  This is not the case in my experience.  I had to abandon this approach as the client continually had problems w/ things like sorting columns and so forth, not to mention getting a numeric looking field to hold leading zeros.  There is a hack for this but it is not very effective.  I finally ditched this and went with jakarta's poi using jython, which works well but not within Zope.
 
Re: Problems with this implemenation by mwr - 2004-10-14
Depending on the Excel versions you need to support, it might be easier
to write an Excel-compatible XML representation of the spreadsheet. At
least with Office XP, I can write an understandable XML file from Excel,
and also read it back in to Excel later.
 
For maximum compatibility, use CSV by acegopher - 2004-10-14
If the above doesn't work for you in Excel, or you don't use Excel, and you don't care about colors or formatting too much, you can use the csv (comma separated values) format.  Use commas to separate your columns, and returns to separate your rows.  Any data with commas in it will have to be double-quoted, but other than that just use the following:

RESPONSE.setHeader("Content-type","text/csv")
RESPONSE.setHeader("Content-disposition","filename=example.csv")

If you do have Excel, it understands and imports the text/csv MIME type.


Excel XML Format by mtb - 2004-10-14
You could do this with ZPT or DTML instead of course, just setting the headers before sending the response.

The data format for modern versions of Excel is XML, so just take a look at an example like the one at http://lists.xml.org/archives/xml-dev/200306/msg00348.html and you'll get the idea.


Also a Python library for this by ianb - 2004-10-14
There's also a Python library that generates native Excel files:

http://sourceforge.net/projects/pyxlwriter/

Unfortunately it requires Python 2.3, so most people wouldn't be able
to run it in the same process as Zope.  It gives access to a lot of 
extra options specific to Excel and spreadsheets.