
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.22 KB | hits: 20 | expires: Never
import sys
import xlrd #http://pypi.python.org/pypi/xlrd
import HTML #http://www.decalage.info/python/html
filename = sys.argv[1]
book = xlrd.open_workbook(filename, formatting_info = True)
for sheet in book.sheets():
table = HTML.Table(width="500px", style="", cellpadding="", border="", attribs={'class': 'financial'})
for r in range(sheet.nrows):
table.rows.append([])
table.rows[r] = [None] * sheet.ncols
for c in range(sheet.ncols):
# TODO switch based on cell type to correctly interpret cell value
cell = sheet.cell(r, c)
xf = book.xf_list[cell.xf_index]
font = book.font_list[xf.font_index]
alignment = xf.alignment
style = ""
if alignment.hor_align in [0,1]:
style += "text-align: left;"
elif alignment.hor_align == 2:
style += "text-align: center;"
elif alignment.hor_align == 3:
style += "text-align: right;"
else:
raise Exception("Unknown hor_align value %s" % alignment.hor_align)
bold = font.weight > 400
if bold:
style += "font-weight: bold;"
cell = HTML.TableCell(cell.value, style=style)
table.rows[r][c] = cell
print table
print "\n\n\n\n"