Writing to Excel
Thomas Guettler
zopestoller at thomas-guettler.de
Thu Jun 20 06:34:31 EDT 2002
Chris wrote:
> Hi I'm using a Python cgi script to write the contents of a table to
> an Excel sheet. I retrieve all the information fine but it seems like
> Excel doesnt want to print more than one line for me. It prints
> 'headerLine' but nothing more. Why???
>
> ...
> headerLine = "some crap"
>
> sys.stdout.write( Html.header( 'application/x-msexcel' ) )
> print string.strip(headerLine)
> allText = ""
I do it with a different content-type. The code is attached, maybe
it helps
thomas
+++
if REQUEST:
REQUEST.RESPONSE.setHeader('Content-Type',
'application/vnd.ms-excel')
output=[]
output.append("<html><body><table border='1'>\n")
headers=list_of_all_properties.keys()
headers.sort()
output.append('<tr>')
for header in headers:
output.append('<th>%s</th>' % cgi.escape(header))
output.append('</tr>\n\n')
for line in lines:
output.append('<tr>')
for col in headers:
value=line.get(col, '')
if type(value) in [type([]), type(())]:
new_value=[]
for keyword in value:
new_value.append(cgi.escape(keyword))
value=string.join(new_value, '<br>')
else:
value=cgi.escape(value)
output.append("<td>%s</td>" % value)
output.append('</tr>\n\n')
output.append("</table></body></html>")
return string.join(output)
+++
More information about the Python-list
mailing list