1

I am using xlwings 0.23.1 to work with MS excel 365.

but when I execute the following code:

import xlwings as xw

wb = xw.books.active
ws = wb.sheets('MySheet')
tbl = ws.api.ListObjects('MyTable') # or .ListObjects(1)
rng = ws.range(tbl.range.address) # get range from table address

df = rng.options(pd.DataFrame, header=True).value # load range to dataframe

the above code stucks at

 rng = ws.range(tbl.range.address) # get range from table address

and does not exceute.

The code is taken from: Read Excel Table headers with xlwings

I am using Spyder IDE

1 Answers1

1

With my local installation xlwings 0.16.0, your code works; but it fails as you described after upgrading to 0.23.0. It seems after a certain version, xlwings strictly separates xlwings object and the associated win32com object (generally converted by .api).

A fix is changing the line from

rng = ws.range(tbl.range.address) # get range from table address

to (Capitalize range and address)

rng = ws.range(tbl.Range.Address) # get range from table address

For a win32com object, its api usually capitalizes the first letter, while all lower cases for xlwings. The code in third line ws.api returns a win32com object, then tbl, so .range.address is affected accordingly.

skyway
  • 171
  • 1
  • 7