I am trying to figure out how to resize a table using xlwings but can't figure out how.
I have tried using the resize(range)
property but I am getting getting the following error :
AttributeError: 'str' object has no attribute 'api'
This is the code I got the error with :
import xlwings as xw
tableau = xw.books['test_book.xlsx'].sheets[0].tables[0]
tableau.resize('<Range [test_book.xlsx]Feuil1!$A$1:$B$6>')
I tried different values for the range attribut like $A$1:$B$6
or A1:B6
but still couldn't make it work.
So how can I manage to resize my table using xlwings ?
CodePudding user response:
Shouldn't need to, the table is resized automatically.
This is the code I ran for your previous question using an Excel file with a table, 'Table1' consisting of 3 columns and 3 rows with header row so the table range is A1:C4. The code adds two additional rows as individual cells and as a tuple. As each row is added the size of the table increases to the include the added rows.
import xlwings as xw
workbook = 'table1.xlsx'
wb = xw.Book(workbook)
ws = wb.sheets('Sheet1')
print("Original Table")
table = ws.cells.table
print("Table Range: " str(table.range.address))
# Added data by cell
ws.range('A5').value = '444'
ws.range('B5').value = '333'
ws.range('C5').value = '222'
table2 = ws.cells.table
print("Table Range after 1 row added: " str(table2.range.address))
### Added row
ws['A6'].value = [111, 999, 987]
table3 = ws.cells.table
print("Table Range after 2nd row added: " str(table3.range.address))
wb.save(workbook)
wb.close()
Ouput
Original Table
Table Range: $A$1:$C$4
Added rows
Table Range after 1 row added: $A$1:$C$5
Table Range after 2nd row added: $A$1:$C$6