Load password protected Excel files into Pandas DataFrame

1 minute read

When trying to read an Excel file into a Pandas DataFrame gives you the following error, the issue might be that you are dealing with a password protected Excel file.

pd.read_excel(PATH)
[...]
XLRDError: Can't find workbook in OLE2 compound document

There seems to be no way of reading password protected files with xlrd.

xlwings for the rescue

Fortunately, there is xlwings, which lets you interact with the Excel application itself (via pywin32 or appscript).

The following code will open your Excel file (if not open already, it will launch the Excel app, which then asks for your password) and turn a range selection of a sheet into a Pandas DataFrame.

Note: You need to have Excel installed. The code below was tested on macOS, but Windows should work the same.

import pandas as pd
import xlwings as xw

PATH = '/Users/me/Desktop/xlwings_sample.xlsx'
wb = xw.Book(PATH)
sheet = wb.sheets['sample']

df = sheet['A1:C4'].options(pd.DataFrame, index=False, header=True).value
df

Output:

Name City Country
0 Jane San Francisco US
1 John London UK
2 David Hamburg DE

Your use case

A couple of things you might want to adjust for your case:

  • A1:C4 is the selection you want to access (from cell A, first row, to cell C, fourth row), one-based index
  • index=False means you don’t want to use the cell values as index
  • header=True means you want to use the first row cell values as header

Like to comment? Feel free to send me an email or reach out on Twitter.

Did this or another article help you? If you like and can afford it, you can buy me a coffee (3 EUR) ☕️ to support me in writing more posts. In case you would like to contribute more or I helped you directly via email or coding/troubleshooting session, you can opt to give a higher amount through the following links or adjust the quantity: 50 EUR, 100 EUR, 500 EUR. All links redirect to Stripe.