Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.22 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import sys
  2.  
  3. import xlrd  #http://pypi.python.org/pypi/xlrd
  4. import HTML  #http://www.decalage.info/python/html
  5.  
  6.  
  7. filename = sys.argv[1]
  8. book = xlrd.open_workbook(filename, formatting_info = True)
  9.  
  10. for sheet in book.sheets():
  11.   table = HTML.Table(width="500px", style="", cellpadding="", border="", attribs={'class': 'financial'})
  12.  
  13.   for r in range(sheet.nrows):
  14.     table.rows.append([])
  15.     table.rows[r] = [None] * sheet.ncols
  16.     for c in range(sheet.ncols):
  17.       # TODO switch based on cell type to correctly interpret cell value
  18.       cell = sheet.cell(r, c)
  19.       xf = book.xf_list[cell.xf_index]
  20.       font = book.font_list[xf.font_index]
  21.       alignment = xf.alignment
  22.       style = ""
  23.       if alignment.hor_align in [0,1]:
  24.         style += "text-align: left;"
  25.       elif alignment.hor_align == 2:
  26.         style += "text-align: center;"
  27.       elif alignment.hor_align == 3:
  28.         style += "text-align: right;"
  29.       else:
  30.         raise Exception("Unknown hor_align value %s" % alignment.hor_align)
  31.       bold = font.weight > 400
  32.       if bold:
  33.         style += "font-weight: bold;"
  34.       cell = HTML.TableCell(cell.value, style=style)
  35.       table.rows[r][c] = cell
  36.  
  37.   print table
  38.   print "\n\n\n\n"