Explain Codes LogoExplain Codes Logo

Xlrd.biffh.xlrderror: Excel xlsx file; not supported

python
pandas
openpyxl
excel
Anton ShumikhinbyAnton Shumikhin·Dec 4, 2024
TLDR

To eliminate the xlrd.biffh.XLRDError, switch to openpyxl for .xlsx file handling:

from openpyxl import load_workbook wb = load_workbook('file.xlsx') print(wb.active['A1'].value)

Install it by running pip install openpyxl. Since version 2.0.0, xlrd has ceased to support .xlsx files.

Next, ensure your packages are up-to-date. Most notably, pandas and openpyxl should always be at their freshest:

pip install pandas --upgrade pip install openpyxl

Finally, remember to specify openpyxl as your engine with pandas.read_excel:

import pandas as pd df = pd.read_excel('file.xlsx', engine='openpyxl')

Fear files no more

Fear not file paths, anymore! With os.path.join, your file paths stay error-free across various operating systems:

import os file_path = os.path.join('folder', 'file.xlsx') # Like a friendly path builder! df = pd.read_excel(file_path, engine='openpyxl')

Safe updates are smart updates

Updated your packages? Cool! But beware of security implications when updating xlrd. For older codes needing xlrd, ensure you don't exceed version 1.2.0:

pip install 'xlrd==1.2.0'

This however should still be approached with caution, because who knows! Old versions could be rusty.

Pandas: Brings clarity and cuddles

Embrace the change with Pandas 1.0.1 or newer versions and enjoy a smoother ride through your Excel spreadsheet manipulation journey. With better integration with openpyxl, concise and robust code is just a few keystrokes away.

Official guides are your best mates

Consult official Pandas and openpyxl documentation often for clear instructions on handling Excel files. If you're wrestling with macro-enabled workbooks, these guides will be your best defense.

df = pd.read_excel('macro_file.xlsm', engine='openpyxl')

Just like that, handling .xlsm files, that pack macros, is no tougher than your regular .xlsx files.

Don't fight the openpyxl

Adopt the versatility of openpyxl that not only allows you to read but also write and navigate through xlsx files. It's like swapping an old rusty key for a Swiss army knife!

Meeting sheets and cells

Navigating through workbooks is a breeze with openpyxl’s intuitive structure:

sheet = wb["Sheet1"] # Ask nicely for the sheet! cell = sheet['A1'] print(cell.value)

No need to break your head, access and manipulate Excel data like a breeze with openpyxl.

Honourable mentions

Shoutout to these excellent libraries, if you need more specialized tools:

  • XlsxWriter for creating Excel files with complex, 'Picasso level' formatting.
  • pyxlsb for when you've to deal with binary Excel files (.xlsb).

Remember, the right tool makes the code cool!