class Sheet(object): def __init__(self, name): self._table = Table(name=name) self._rows = {} def create_row(self, name, **kwargs): """Create an empty row to manually insert cells""" self._rows[name] = Row(**kwargs) self._table.addElement(self._rows[name]._row) return self._rows[name] def write_row(self, name, cells, row_styles={}, cell_styles={}): """Create a new row and populate it with the given cells""" row = self.create_row(name, **row_styles) row.write_cells(cells, **cell_styles) def get_row(self, name): return self._rows[name] def create_column(self, **kwargs): column = TableColumn(**kwargs) self._table.addElement(column) def read_cell(self, x, y): try: cell = self._table.getElementsByType(TableRow)[y].childNodes[x] except IndexError: return '' result = [] for element in cell.childNodes: result.append("".join([v.data for v in element.childNodes])) return "\n".join(result)
ods_doc = OpenDocumentSpreadsheet() # Create a new Table/Sheet sheet = Table(name="odfpy_ods") # Create a new empty row tablerow = TableRow() # Create 3 new cells with different value type cell1 = TableCell(valuetype="string") cell1.addElement(P(text="Hello World !")) cell2 = TableCell(valuetype="float", value=1) cell3 = TableCell(valuetype="currency", currency="CHF", value="-8100") # Add these cells to the row tablerow.addElement(cell1) tablerow.addElement(cell2) tablerow.addElement(cell3) # Add the row to the table sheet.addElement(tablerow) # Add the table/sheet to the ods document ods_doc.spreadsheet.addElement(sheet) # Save the document ods_doc.save(r"C:\FichiersLocauxRevit\odfpy.ods") # Load an ods document ods_doc = load(r"C:\FichiersLocauxRevit\odfpy.ods") # Get first sheet sheet = ods_doc.getElementsByType(Table)[0] # Get cell a i, j (row, column) i, j = 1, 2 row = sheet.getElementsByType(TableRow)[i - 1] cell = row.getElementsByType(TableCell)[j - 1] print cell.value