def render(self, request, context, **response_kwargs): from xlwt import Workbook, XFStyle, easyxf w = Workbook(encoding='utf-8') 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, format='xls'), 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 test_with_widget(): c = Column('user.first_name') r = RowValue('Test', column=c) assert with_widget(r) == 'Test' e = RowValueError('Test') assert with_widget(e) == 'Test' with pytest.raises(ValueError): with_widget('Wrong')
def test_with_widget(): c = Column('user.first_name') r = RowValue('Test', column=c) assert with_widget(r) == 'Test' e = RowValueError('Test') assert with_widget(e) == 'Test' with pytest.raises(ValueError): with_widget('Wrong')
def test_with_widget(): c = Column('user.first_name') r = RowValue('Test', column=c) assert with_widget(r) == 'Test' assert with_widget(r, format='foo') == 'Test' c = Column('normal', format="%Y-%m-%d") r = RowValue(datetime.date(2000, 1, 1), column=c) assert with_widget(r, format='xls') == datetime.date(2000, 1, 1) e = RowValueError('Test') assert with_widget(e) == 'Test' with pytest.raises(ValueError): with_widget('Wrong')
def test_with_widget(): c = Column('user.first_name') r = RowValue('Test', column=c) assert with_widget(r) == 'Test' assert with_widget(r, format='foo') == 'Test' c = Column('normal', format="%Y-%m-%d") r = RowValue(datetime.date(2000, 1, 1), column=c) assert with_widget(r, format='xls') == datetime.date(2000, 1, 1) e = RowValueError('Test') assert with_widget(e) == 'Test' with pytest.raises(ValueError): with_widget('Wrong')