Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Excel Table headers with xlwings

How can I use xlwings to read a "table" in excel, into a pandas DataFrame, where the table "headers" become the DataFrame column names?

Every way I have tried to read the table, the header row is always excluded from the read!

Here is what I've tried, where "b" is my xlwings workbook object:

b.sheets['Sheet1'].range('Table1').options(pd.DataFrame)
b.sheets['Sheet1'].range('Table1').options(pd.DataFrame, headers=False)
b.sheets['Sheet1'].range('Table1').options(pd.DataFrame, headers=True)
like image 256
CrashLandon Avatar asked Nov 02 '25 03:11

CrashLandon


2 Answers

Hoping this is not the best answer, but I did find I could reference the named range, then .offset(-1).expand('vertical')

like image 58
CrashLandon Avatar answered Nov 04 '25 19:11

CrashLandon


Another option is to use the api and Excel's ListObject

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
like image 34
Jayme Gordon Avatar answered Nov 04 '25 20:11

Jayme Gordon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!