|
|
|
|
@ -1,5 +1,9 @@ |
|
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
http://www.simplistix.co.uk/presentations/python-excel.pdf |
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
import xlwt |
|
|
|
|
import datetime |
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist |
|
|
|
|
@ -12,6 +16,7 @@ CELL_STYLE_MAP = ( |
|
|
|
|
(bool, xlwt.easyxf(num_format_str='BOOLEAN')), |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def multi_getattr(obj, attr, default=None): |
|
|
|
|
attributes = attr.split(".") |
|
|
|
|
for i in attributes: |
|
|
|
|
@ -39,33 +44,64 @@ def get_column_cell(obj, name): |
|
|
|
|
return attr |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def queryset_to_workbook(queryset, columns, header_style=None, default_style=None, cell_style_map=None): |
|
|
|
|
def queryset_to_workbook(queryset, columns, report_date = None): |
|
|
|
|
|
|
|
|
|
# defining styles for different types of cells |
|
|
|
|
main_style = xlwt.Style.easyxf( |
|
|
|
|
"font: name Calibri, height 600, bold False;" |
|
|
|
|
"borders: left thin, right thin, top thin, bottom thin;" |
|
|
|
|
"alignment: horizontal left, vertical center, indent 7;" |
|
|
|
|
"pattern: pattern solid, fore_colour white;" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
header_style = xlwt.Style.easyxf( |
|
|
|
|
'font: name Calibri, height 400, bold False;' |
|
|
|
|
'borders: left no_line, right no_line, top thin, bottom thin;' |
|
|
|
|
'alignment: horizontal center, shrink_to_fit True;' |
|
|
|
|
'pattern: pattern solid, fore_color gray_ega;', |
|
|
|
|
) |
|
|
|
|
odd_style = xlwt.Style.easyxf( |
|
|
|
|
'font: name Calibri, height 300, bold False;' |
|
|
|
|
'borders: left thin, right thin, top thin, bottom thin;' |
|
|
|
|
'alignment: horizontal center, wrap True;' |
|
|
|
|
'pattern: pattern solid, fore_color white;', |
|
|
|
|
) |
|
|
|
|
even_style = xlwt.Style.easyxf( |
|
|
|
|
'font: name Calibri, height 300, bold False;' |
|
|
|
|
'borders: left thin, right thin, top thin, bottom thin;' |
|
|
|
|
'alignment: horizontal center, wrap True;' |
|
|
|
|
'pattern: pattern solid, fore_color silver_ega;', |
|
|
|
|
) |
|
|
|
|
# creating workbook and adding sheet |
|
|
|
|
workbook = xlwt.Workbook() |
|
|
|
|
report_date = datetime.date.today() |
|
|
|
|
sheet_name = u'My calendar {0}'.format(report_date.strftime('%Y-%m-%d')) |
|
|
|
|
report_date = report_date or datetime.date.today() |
|
|
|
|
sheet_name = u'My calendar {0}'.format(report_date.strftime('%Y-%B')) |
|
|
|
|
sheet = workbook.add_sheet(sheet_name) |
|
|
|
|
sheet.insert_bitmap('') |
|
|
|
|
|
|
|
|
|
if not header_style: |
|
|
|
|
header_style = HEADER_STYLE |
|
|
|
|
if not default_style: |
|
|
|
|
default_style = DEFAULT_STYLE |
|
|
|
|
if not cell_style_map: |
|
|
|
|
cell_style_map = CELL_STYLE_MAP |
|
|
|
|
|
|
|
|
|
obj = queryset[0] |
|
|
|
|
# drawing head part with image |
|
|
|
|
sheet.write_merge(0, 6, 0, 6, u'Мой календарь собитий на %s года' % report_date.strftime("%B %Y"), main_style) |
|
|
|
|
for i in range(7): |
|
|
|
|
sheet.row(i).set_style(xlwt.Style.easyxf('font:height 300;')) |
|
|
|
|
sheet.insert_bitmap('/home/www/proj/media/logo.bmp', row=0, col=5, x=0, y=0, scale_x=0.3, scale_y=2) |
|
|
|
|
|
|
|
|
|
# drawing headers |
|
|
|
|
header_list = [u'#', u'Название события',u'Даты',u'Краткое описание',u'Место проведения', u'Заметка', u'Ссылка на событие'] |
|
|
|
|
for i, column in enumerate(columns): |
|
|
|
|
header_list=[u'#', u'Название события',u'Даты',u'Краткое описание',u'Место проведения', u'Заметка', u'Ссылка на событие'] |
|
|
|
|
sheet.write(0, i, header_list[i], header_style) |
|
|
|
|
sheet.write(8, i, header_list[i], header_style) |
|
|
|
|
sheet.col(i).width = 8000 |
|
|
|
|
sheet.col(0).width = 2000 |
|
|
|
|
|
|
|
|
|
for x, obj in enumerate(queryset, start=1): |
|
|
|
|
# fill data |
|
|
|
|
for x, obj in enumerate(queryset, start=9): |
|
|
|
|
for y, column in enumerate(columns): |
|
|
|
|
value = getattr(obj, column) |
|
|
|
|
style = default_style |
|
|
|
|
for value_type, cell_style in cell_style_map: |
|
|
|
|
if isinstance(value, value_type): |
|
|
|
|
style = cell_style |
|
|
|
|
try: |
|
|
|
|
value = getattr(obj, column) |
|
|
|
|
except: |
|
|
|
|
value = "-" |
|
|
|
|
if x % 2 == 0: |
|
|
|
|
style = even_style |
|
|
|
|
else: |
|
|
|
|
style = odd_style |
|
|
|
|
sheet.write(x, y, value, style) |
|
|
|
|
|
|
|
|
|
return workbook |
|
|
|
|
|