def write(self, col, label, style=Style.default_style): self.__adjust_height(style) self.__adjust_bound_col_idx(col) style_index = self.__parent_wb.add_style(style) if isinstance(label, basestring): if len(label) > 0: self.insert_cell( col, StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label))) else: self.insert_cell(col, BlankCell(self.__idx, col, style_index)) elif isinstance(label, bool): # bool is subclass of int; test bool first self.insert_cell(col, BooleanCell(self.__idx, col, style_index, label)) elif isinstance(label, (float, int, long, Decimal)): self.insert_cell(col, NumberCell(self.__idx, col, style_index, label)) elif isinstance(label, (dt.datetime, dt.date, dt.time)): date_number = self.__excel_date_dt(label) self.insert_cell( col, NumberCell(self.__idx, col, style_index, date_number)) elif label is None: self.insert_cell(col, BlankCell(self.__idx, col, style_index)) elif isinstance(label, ExcelFormula.Formula): self.__parent_wb.add_sheet_reference(label) self.insert_cell(col, FormulaCell(self.__idx, col, style_index, label)) elif isinstance(label, (list, tuple)): self.__rich_text_helper(col, label, style, style_index) else: raise Exception("Unexpected data type %r" % type(label))
def __rich_text_helper(self, col, rich_text_list, style, style_index=None): if style_index is None: style_index = self.__parent_wb.add_style(style) default_font = None rt = [] for data in rich_text_list: if isinstance(data, basestring): s = data font = default_font elif isinstance(data, (list, tuple)): if not isinstance(data[0], basestring) or not isinstance( data[1], Font): raise Exception("Unexpected data type %r, %r" % (type(data[0]), type(data[1]))) s = data[0] font = self.__parent_wb.add_font(data[1]) else: raise Exception("Unexpected data type %r" % type(data)) if s: rt.append((s, font)) if default_font is None: default_font = self.__parent_wb.add_font(style.font) if rt: self.insert_cell( col, StrCell(self.__idx, col, style_index, self.__parent_wb.add_rt(rt))) else: self.insert_cell(col, BlankCell(self.__idx, col, style_index))
def set_cell_text(self, colx, value, style=Style.default_style): self.__adjust_height(style) self.__adjust_bound_col_idx(colx) xf_index = self.__parent_wb.add_style(style) self.insert_cell( colx, StrCell(self.__idx, colx, xf_index, self.__parent_wb.add_str(value)))