class ExcelReport(BaseReport):
""" MS Excel Style Reports """
workbook = None
tempf = None
CHAR_WIDTH = 275
def __init__(self):
self.workbook = xl.Workbook()
self.workbook._Workbook__use_cell_values = 0
self.cellformatters = {}
super(ExcelReport, self).__init__()
def __del__(self):
if self.tempf and not self.tempf.closed:
self.tempf.close()
def addCellFormatter(self,CellFormatter):
self.cellformatters[self.current_sheet] = CellFormatter
def getCellFormatter(self):
return self.cellformatters
def nextsheet(self,name=None):
"""
Deprecated. Votovox is using this.
"""
self.addSheet(name)
def output(self):
"""
Deprecated. Votovox is using this.
"""
self.writeReport(self)
def writeReport(self):
if not self.tempf:
self.tempf = StringIO.StringIO()
for k, v in self.grids.iteritems():
self.current_x = 0
self.current_y = 0
ws = self.workbook.add_sheet(k)
grid = v.getGrid()
for col in grid:
header_value = ''
for item in col:
if item.get_type() == ITEM_HEADER:
header_value = item.get_value()
if self.getFormatter()[k] != {}:
if item.get_type() == ITEM_HEADER and self.getFormatter()[k].header_style is not None:
self.style = self.getFormatter()[k].getHeaderStyle()
elif self.getFormatter()[k].col_styles != {} and self.getFormatter()[k].getColumnStyle().has_key(header_value):
self.style = self.getFormatter()[k].getColumnStyle(header_value)
elif self.getFormatter()[k].alternate_color_style is not None and not helpers.isodd(self.current_x):
self.style = self.getFormatter()[k].getAlternateColorStyle()
elif self.getFormatter()[k].body_style is not None:
self.style = self.getFormatter()[k].getBodyStyle()
else:
self.style = ExcelStyle().get_excel_style()
if self.getFormatter()[k].getWidth() != {}:
if self.getFormatter()[k].getWidth().has_key(header_value):
setwidth = self.getFormatter()[k].getWidth()[header_value]
self.__adjustwidth(item,setwidth)
else:
self.__adjustwidth(item)
else:
self.__adjustwidth(item)
self.style = ExcelStyle().get_excel_style()
if self.get_format(item) != self.formatters['DEFAULT']:
self.style.num_format_str = self.get_format(item)
ws.write(
self.current_x,
self.current_y,
item.get_value(),
self.style
)
self.change_column_position()
if self.getFormatter()[k] != {}:
if self.getFormatter()[k].getFormula().has_key(header_value):
excel_formula = '%s(%s%s:%s%s)' % (self.formatter.getFormula()[k][header_value],
helpers.convert_to_letter(self.current_y),'2',
helpers.convert_to_letter(self.current_y),
self.current_x - 1)
self.style = ExcelStyle().get_excel_style()
ws.write(self.current_x,self.current_y,xl.Formula(excel_formula),self.style)
self.change_row_position()
self.workbook.save(self.tempf)
return self.tempf.getvalue()
def __func_for_nums(item):
i = 0
style = '0.'
while i < item.get_percision():
style += '0'
i += 1
return style
formatters = {
ITEM_DATETIME: 'M/D/YYYY h:mm:ss',
ITEM_DATE: 'M/D/YYYY',
ITEM_TIME: 'h:mm:ss',
ITEM_FLOAT: __func_for_nums,
ITEM_DECIMAL: __func_for_nums,
ITEM_INT: '0',
'DEFAULT': u'',
}
# Private internal methods
def __cellcord(self):
return self.current_x,self.current_y
def __cursheet(self):
return self.workbook.get_sheet(self.sheetcount-2)
def __adjustwidth(self,item,setwidth=0):
newlen = item.get_length() * self.CHAR_WIDTH
if self.__cursheet().col(self.current_x).width < newlen:
if setwidth != 0:
newlen = setwidth
self.__cursheet().col(self.current_y).width = newlen