Example #1
0
    #  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()))
    print("Content of the table:")
    print(table.to_csv())

    if not os.path.exists('test_output'):
        os.mkdir('test_output')

    output = os.path.join('test_output', "my_basic_spreadsheet.ods")

    document.save(target=output, pretty=True)
    table2.set_value('A2', "range:")
    table2.set_value('B2', str(named_range1.crange))
    table2.set_value('A3', "from table:")
    table2.set_value('B3', named_range1.table_name)
    table2.set_value('A4', "content:")
    table2.set_value('B4', named_range1.get_value())

    named_range2 = table2.get_named_range('squares_values')
    table2.set_value('D1', "name:")
    table2.set_value('E1', named_range2.name)
    table2.set_value('D2', "range:")
    table2.set_value('E2', str(named_range2.crange))
    table2.set_value('D3', "from table:")
    table2.set_value('E3', named_range2.table_name)
    table2.set_value('D4', "content:")
    # using "E4:4" notaion is a little hack for the area starting at E4 on row 4
    table2.set_values(values=[named_range2.get_values(flat=True)],
                      coord='E4:4')

    print("Content of the table1:")
    print(table.name)
    print(table.to_csv())
    print(table2.name)
    print(table2.to_csv())

    # of course named ranges are stored in the document :
    if not os.path.exists('test_output'):
        os.mkdir('test_output')
    output = os.path.join('test_output', "my_spreadsheet_with_named_range.ods")
    document.save(target=output, pretty=True)