def render(self, request, context, **response_kwargs): from xlwt import Workbook, XFStyle, easyxf w = Workbook(encoding='utf-8') w.owner = b'UN World Food Programme' ws = w.add_sheet('Report') style = XFStyle() row = 0 heading_xf = easyxf( 'font:height 200; font: bold on; align: wrap on, vert centre, horiz center' ) ws.write(row, 0, '#', style) for col, fieldname in enumerate(context['report'].headers, start=1): ws.write(row, col, str(fieldname), heading_xf) ws.col(col).width = 5000 ws.row(row).height = 500 # we have to prepare all the styles before going into the loop # to avoid the "More than 4094 XFs (styles)" Error styles = self._get_styles(context) for rownum, data in enumerate(context['report']): ws.write(rownum + 1, 0, rownum + 1) for idx, (fieldname, rowvalue) in enumerate(data.items()): style = styles[rowvalue.column.name] try: ws.write(rownum + 1, idx + 1, with_widget(rowvalue), style) except Exception: #logger.warning("TODO refine this exception: %s" % e) ws.write(rownum + 1, idx + 1, smart_str(with_widget(rowvalue)), style) f = StringIO.StringIO() w.save(f) f.seek(0) return f.read()
def render(self, request, context, **response_kwargs): from xlwt import Workbook, XFStyle, easyxf w = Workbook(encoding='utf-8') w.owner = b'UN World Food Programme' ws = w.add_sheet('Report') style = XFStyle() row = 0 heading_xf = easyxf('font:height 200; font: bold on; align: wrap on, vert centre, horiz center') ws.write(row, 0, '#', style) for col, fieldname in enumerate(context['report'].headers, start=1): ws.write(row, col, str(fieldname), heading_xf) ws.col(col).width = 5000 ws.row(row).height = 500 # we have to prepare all the styles before going into the loop # to avoid the "More than 4094 XFs (styles)" Error styles = self._get_styles(context) for rownum, data in enumerate(context['report']): ws.write(rownum + 1, 0, rownum + 1) for idx, (fieldname, rowvalue) in enumerate(data.items()): style = styles[rowvalue.column.name] try: ws.write(rownum + 1, idx + 1, with_widget(rowvalue), style) except Exception: #logger.warning("TODO refine this exception: %s" % e) ws.write(rownum + 1, idx + 1, smart_str(with_widget(rowvalue)), style) f = StringIO.StringIO() w.save(f) f.seek(0) return f.read()