Advertisement
marlob

writexls

May 2nd, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # Write an XLS file with a single worksheet, containing
  2. # a heading row and some rows of data.
  3.  
  4. import xlwt
  5. import datetime
  6. import lepl
  7.  
  8.  
  9. ezxf = xlwt.easyxf
  10.  
  11. def write_xls(file_name, sheet_name, headings, heading_xf, data_xfs):
  12.     number = lepl.Real()
  13.     spaces = ~lepl.Star(lepl.Space())
  14.     calls = lepl.Literal("call")
  15.     with lepl.Separator(spaces):
  16.         fctmmi = ~spaces & ~calls & lepl.Word() & ~lepl.Literal("(") & lepl.String() & ~lepl.Literal(",") & lepl.String() & ~lepl.Literal(",") & number & ~lepl.Literal(")")
  17.  
  18.     book = xlwt.Workbook()
  19.     sheet = book.add_sheet(sheet_name)
  20.     rowx = 0
  21.     for colx, value in enumerate(headings):
  22.         sheet.write(rowx, colx, value, heading_xf)
  23.     sheet.set_panes_frozen(True) # frozen headings instead of split panes
  24.     sheet.set_horz_split_pos(rowx+1) # in general, freeze after last heading row
  25.     sheet.set_remove_splits(True) # if user does unfreeze, don't leave a split there
  26.  
  27.     try:
  28.         with open("m7110426_baitt.mps", 'rb') as f:
  29.             for index, line in enumerate(f):
  30.                 decline = line.decode('iso-8859-1').strip()
  31.                 try:
  32.                     result = fctmmi.parse(decline)
  33.                     #print result
  34.                     rowx += 1
  35.                     for colx, value in enumerate(result):
  36.                         sheet.write(rowx, colx, value, data_xfs[colx])
  37.                 except lepl.stream.maxdepth.FullFirstMatchException:
  38.                     pass
  39.  
  40.     except IOError:
  41.         pass
  42.  
  43.  
  44.     book.save(file_name)
  45.  
  46. if __name__ == '__main__':
  47.     mkd = datetime.date
  48.     hdngs = ['function', 'tag', 'description', 'Id']
  49.     kinds =  'text        text   text           int'.split()
  50.     data = [['MMI_IniWIC', 'WIC3.0', 'Weegschaal 3 chem. gewicht  ( 3)', '3'] ]
  51.  
  52.     heading_xf = ezxf('font: bold on; align: wrap on, vert centre, horiz center')
  53.     kind_to_xf_map = {
  54.         'date': ezxf(num_format_str='yyyy-mm-dd'),
  55.         'int': ezxf(num_format_str='#,##0'),
  56.         'money': ezxf('font: italic on; pattern: pattern solid, fore-colour grey25',
  57.             num_format_str='$#,##0.00'),
  58.         'price': ezxf(num_format_str='#0.000000'),
  59.         'text': ezxf(),
  60.         }
  61.     data_xfs = [kind_to_xf_map[k] for k in kinds]
  62.     write_xls('flink_devices.xls', 'Devices', hdngs, heading_xf, data_xfs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement