def add_title(self, title, width): row = TableRow() cell = TableCell(stylename="title") cell.setAttrNS(TABLENS, "number-columns-spanned", width) cell.addElement(P(text=title)) row.addElement(cell) self.sheet.addElement(row)
def write_cell(self, row, x): tc = TableCell() x_type = type(x) x_odf_type = ODS_WRITE_FORMAT_COVERSION.get(x_type, "string") tc.setAttrNS(OFFICENS, "value-type", x_odf_type) x_odf_value_token = VALUE_TOKEN.get(x_odf_type, "value") converter = ODS_VALUE_CONVERTERS.get(x_odf_type, None) if converter: x = converter(x) if x_odf_type != 'string': tc.setAttrNS(OFFICENS, x_odf_value_token, x) tc.addElement(P(text=x)) row.addElement(tc)
def write_cell(self, row, cell): tc = TableCell() cell_type = type(cell) cell_odf_type = ODS_WRITE_FORMAT_COVERSION.get(cell_type, "string") tc.setAttrNS(OFFICENS, "value-type", cell_odf_type) cell_odf_value_token = VALUE_TOKEN.get(cell_odf_type, "value") converter = ODS_VALUE_CONVERTERS.get(cell_odf_type, None) if converter: cell = converter(cell) if cell_odf_type != 'string': tc.setAttrNS(OFFICENS, cell_odf_value_token, cell) tc.addElement(P(text=cell)) else: lines = cell.split('\n') for line in lines: tc.addElement(P(text=line)) row.addElement(tc)
def write_cell(self, value, **kwargs): if "numbercolumnsspanned" in kwargs or "numberrowsspanned" in kwargs: kwargs.setdefault("numberrowsspanned", "1") kwargs.setdefault("numbercolumnsspanned", "1") cell = TableCell(**kwargs) cell.setAttrNS(OFFICENS, "value-type", "string") if isinstance(value, Element): para = P() para.addElement(value) cell.addElement(para) else: for line in value.split("\n"): cell.addElement(P(text=line)) self._row.addElement(cell)
def write_cell(self, row, cell): """write a native cell""" cell_to_be_written = TableCell() cell_type = type(cell) cell_odf_type = ODS_WRITE_FORMAT_COVERSION.get(cell_type, "string") cell_to_be_written.setAttrNS(OFFICENS, "value-type", cell_odf_type) cell_odf_value_token = VALUE_TOKEN.get(cell_odf_type, "value") converter = ODS_VALUE_CONVERTERS.get(cell_odf_type, None) if converter: cell = converter(cell) if cell_odf_type != 'string': cell_to_be_written.setAttrNS(OFFICENS, cell_odf_value_token, cell) cell_to_be_written.addElement(P(text=cell)) else: lines = cell.split('\n') for line in lines: cell_to_be_written.addElement(P(text=line)) row.addElement(cell_to_be_written)
def write_cell(self, row, cell): """write a native cell""" cell_to_be_written = TableCell() cell_type = type(cell) cell_odf_type = converter.ODS_WRITE_FORMAT_COVERSION.get( cell_type, "string") cell_to_be_written.setAttrNS(OFFICENS, "value-type", cell_odf_type) cell_odf_value_token = converter.VALUE_TOKEN.get( cell_odf_type, "value") converter_func = converter.ODS_VALUE_CONVERTERS.get( cell_odf_type, None) if converter_func: cell = converter_func(cell) if cell_odf_type != 'string': cell_to_be_written.setAttrNS(OFFICENS, cell_odf_value_token, cell) cell_to_be_written.addElement(P(text=cell)) else: lines = cell.split('\n') for line in lines: cell_to_be_written.addElement(P(text=line)) row.addElement(cell_to_be_written)
def _get_cell(self, label, stylename=None): """ Build a TableCell and adapt the format to the provided label format :param label: The data to write (int/float/bool/date/str/unicode) :param str stylename: One of the stylenames added in the __init__ :returns: A TableCell instance """ if stylename is not None: cell_to_be_written = TableCell(stylename=stylename) else: cell_to_be_written = TableCell() cell_type = type(label) cell_odf_type = converter.ODS_WRITE_FORMAT_COVERSION.get( cell_type, "string" ) cell_to_be_written.setAttrNS(OFFICENS, "value-type", cell_odf_type) cell_odf_value_token = converter.VALUE_TOKEN.get( cell_odf_type, "value", ) converter_func = converter.ODS_VALUE_CONVERTERS.get( cell_odf_type, None ) if converter_func: label = converter_func(label) if cell_odf_type != 'string': cell_to_be_written.setAttrNS(OFFICENS, cell_odf_value_token, label) cell_to_be_written.addElement(P(text=label)) else: lines = label.split('\n') for line in lines: cell_to_be_written.addElement(P(text=line)) return cell_to_be_written
def write(self, cell, value, style={}): row, col = cell if row >= self._rows: self._rows = row + 1 if col >= self._cols: self._cols = col + 1 tc = TableCell(stylename=self.style('table-cell', style)) if isinstance(value, Formula): tc.setAttrNS(TABLENS, 'formula', value) tc.setAttrNS(OFFICENS, 'value', '0') tc.setAttrNS(OFFICENS, 'value-type', 'float') tc.addElement(P(text=unicode('0', 'utf-8'))) elif isinstance(value, HTML): tc.setAttrNS(OFFICENS, 'value-type', 'string') self._calc.HTML(html5lib.HTMLParser().parse(value), tc) elif isinstance(value, basestring): tc.setAttrNS(OFFICENS, 'value-type', 'string') tc.addElement(P(text=value)) elif isinstance(value, (float, int)): tc.setAttrNS(OFFICENS, 'value-type', 'float') tc.setAttrNS(OFFICENS, 'value', str(value)) tc.addElement(P(text=str(value))) elif isinstance(value, element.Element): tc.setAttrNS(OFFICENS, 'value-type', 'string') tc.addElement(value) self._cells[cell] = tc