table.set_row(row_number, row) # let merge some cells table.set_span((column - 3, row_number, column - 1, row_number), merge=True) # Let's add some style on first row border = make_table_cell_border_string(thick='0.03cm', color='black') cell_style = create_table_cell_style( color='black', background_color=(210, 210, 210), border_right=border, border_left=border, border_bottom=border, border_top=border, ) style_name = commercial.insert_style(style=cell_style, automatic=True) row = table.get_row(0) #for cell in row.get_cells(): #possible, but .traverse() is better for cell in row.traverse(): cell.style = style_name row.set_cell(x=cell.x, cell=cell) table.set_row(row.y, row) if not os.path.exists('test_output'): os.mkdir('test_output') output = os.path.join('test_output', "my_commercial.odt") commercial.save(target=output, pretty=True)
for line in range(lines): row = Row() values = [] n = line for i in range(cols): values.append(n) n = suite(n) row.set_values(values) table.append(row) print("Size of Big Table :", table.size) # now extract 100 rows of 26 columns : table1 = Table("Extract 1") for r in range(800, 900): row = table.get_row(r) values = [row.get_value(x) for x in range(50, 76)] row2 = Row() row2.set_values(values) table1.append(row2) body.append(table1) print("Size of extracted table 1 :", table1.size) # other method table2 = Table("Extract 2") cells = table.get_cells(coord=(50, 800, 75, 899)) table2.set_cells(coord=(2, 3), cells=cells) body.append(table2) print("Size of extracted table 2 :", table2.size)
# is not mandatory, but a good practice, since odfdo don't check # actual existence of cells body = document.body table = Table("First Table", width=20, height=3) body.append(table) # A table contains rows, we can append some more. for r in range(2): table.append_row() print("rows in the table (3+2):", len(table.get_rows())) # A row contains cells for row in table.get_rows(): print("row, nb of cells ", row.y, len(row.get_cells())) last_row = table.get_row(-1) print("nb of cells of the last row:", len(last_row.get_cells())) # cell can have different kind of values for r in range(3): for c in range(10): table.set_value((c, r), "cell %s %s" % (c, r)) for r in range(3, 5): for c in range(10): table.set_value((c, r), c * 100 + r) # Before saving the document, we can strip the unused colums: print("table size:", table.size) table.rstrip() print("table size after strip:", table.size) print("nb of cells of the last row:", len(table.get_row(-1).get_cells()))