Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Write an XLS file with a single worksheet, containing
- # a heading row and some rows of data.
- import xlwt
- import datetime
- import lepl
- ezxf = xlwt.easyxf
- def write_xls(file_name, sheet_name, headings, heading_xf, data_xfs):
- number = lepl.Real()
- spaces = ~lepl.Star(lepl.Space())
- calls = lepl.Literal("call")
- with lepl.Separator(spaces):
- fctmmi = ~spaces & ~calls & lepl.Word() & ~lepl.Literal("(") & lepl.String() & ~lepl.Literal(",") & lepl.String() & ~lepl.Literal(",") & number & ~lepl.Literal(")")
- book = xlwt.Workbook()
- sheet = book.add_sheet(sheet_name)
- rowx = 0
- for colx, value in enumerate(headings):
- sheet.write(rowx, colx, value, heading_xf)
- sheet.set_panes_frozen(True) # frozen headings instead of split panes
- sheet.set_horz_split_pos(rowx+1) # in general, freeze after last heading row
- sheet.set_remove_splits(True) # if user does unfreeze, don't leave a split there
- try:
- with open("m7110426_baitt.mps", 'rb') as f:
- for index, line in enumerate(f):
- decline = line.decode('iso-8859-1').strip()
- try:
- result = fctmmi.parse(decline)
- #print result
- rowx += 1
- for colx, value in enumerate(result):
- sheet.write(rowx, colx, value, data_xfs[colx])
- except lepl.stream.maxdepth.FullFirstMatchException:
- pass
- except IOError:
- pass
- book.save(file_name)
- if __name__ == '__main__':
- mkd = datetime.date
- hdngs = ['function', 'tag', 'description', 'Id']
- kinds = 'text text text int'.split()
- data = [['MMI_IniWIC', 'WIC3.0', 'Weegschaal 3 chem. gewicht ( 3)', '3'] ]
- heading_xf = ezxf('font: bold on; align: wrap on, vert centre, horiz center')
- kind_to_xf_map = {
- 'date': ezxf(num_format_str='yyyy-mm-dd'),
- 'int': ezxf(num_format_str='#,##0'),
- 'money': ezxf('font: italic on; pattern: pattern solid, fore-colour grey25',
- num_format_str='$#,##0.00'),
- 'price': ezxf(num_format_str='#0.000000'),
- 'text': ezxf(),
- }
- data_xfs = [kind_to_xf_map[k] for k in kinds]
- write_xls('flink_devices.xls', 'Devices', hdngs, heading_xf, data_xfs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement