xlrd.biffh.XLRDError: Excel xlsx file; not supported [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# xlrd.biffh.XLRDError: Excel xlsx file; not supported [Fixed]

The error "xlrd.biffh.XLRDError: Excel xlsx file; not supported" occurs because the package xlrd now only supports .xls files.

To solve the error, upgrade your version of pandas, install the openpyxl module and use it to read excel files instead.

shell
File "C:\python\lib\site-packages\xlrd\__init__.py", line 174, in open_workbook raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported') xlrd.biffh.XLRDError: Excel xlsx file; not supported
Starting with pandas 1.2.0, the engine xlrd only supports old-style .xls files because of some security implications.

As shown in the docs, the xlrd library will no longer read anything other than .xls files.

xlrd only reads xls files

# Update pandas and install openpyxl

To resolve the issue:

  1. Open your terminal and upgrade the version of your pandas module.
shell
pip install pandas --upgrade pip3 install pandas --upgrade python -m pip install pandas --upgrade python3 -m pip install pandas --upgrade py -m pip install pandas --upgrade # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install pandas --upgrade

upgrade pandas version

  1. Install the openpyxl module.
shell
pip install openpyxl --upgrade pip3 install openpyxl --upgrade python -m pip install openpyxl --upgrade python3 -m pip install openpyxl --upgrade py -m pip install openpyxl --upgrade # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install openpyxl --upgrade

install openpyxl module

  1. Explicitly set the engine keyword argument to openpyxl in the call to the read_excel() method.
main.py
import pandas as pd df = pd.read_excel( 'example.xlsx', engine='openpyxl' ) print(df)

pandas read excel with engine openpyxl

The code for this article is available on GitHub

The code sample assumes that you have an example.xlsx file in the same directory.

example.xlsx
Name Salary 0 Alice 100 1 Bobby 75 2 Carl 150

Note that openpyxl does not support the old .xls file format.

# Reading a .xls file

If you need to read a .xls file, you have to use xlrd instead. Alternatively, you can convert the file to .xlsx.

Assuming you have a file called another.xls, you can set the engine to xlrd to read it.

main.py
import pandas as pd df = pd.read_excel( 'another.xls', engine='xlrd' ) print(df)

read xls file using xlrd

The code for this article is available on GitHub

If the suggestion of upgrading pandas and using openpyxl didn't work, you could try to downgrade xlrd to the last version that supports reading .xlsx files.

However, before you consider downgrading xlrd, note that this has security implications because files with the .xslx extension may contain scripts.

More recent versions of pandas also throw an error if you have version 1.X of xlrd installed.

# Downgrading to version 1.X of xlrd (NOT RECOMMENDED)

If you are OK with the security risks of using xlrd version 1.X, run the following command.

shell
pip uninstall xlrd pip install xlrd==1.2.0 pip3 uninstall xlrd pip3 install xlrd==1.2.0

If you get an error that ImportError: Pandas requires version '2.X' or newer of 'xlrd' (version '1.2.0' currently installed), upgrade your version of xlrd and pandas and use the openpyxl package to read .xlsx files instead.

shell
pip install pandas xlrd --upgrade pip3 install pandas xlrd --upgrade python -m pip install pandas xlrd --upgrade python3 -m pip install pandas xlrd --upgrade py -m pip install pandas xlrd --upgrade # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install pandas xlrd --upgrade

If you don't have openpyxl installed, run the following command.

shell
pip install openpyxl --upgrade pip3 install openpyxl --upgrade python -m pip install openpyxl --upgrade python3 -m pip install openpyxl --upgrade py -m pip install openpyxl --upgrade # ๐Ÿ‘‡๏ธ For Jupyter Notebook !pip install openpyxl --upgrade

# Set the engine keyword argument to openpyxl

Explicitly set the engine keyword argument to openpyxl in the call to the pandas.read_excel() method.

main.py
import pandas as pd df = pd.read_excel( 'example.xlsx', engine='openpyxl' ) print(df)

pandas read excel with engine openpyxl

The code for this article is available on GitHub

# Make sure your file has the correct extension

Files with .xls and .xlsx extensions are distinct.

If a file has a .xlsx extension, then it might contain executable scripts.

The xlrd library no longer supports files with .xlsx extensions because of the security implications around reading files that might contain executable scripts.

The openpyxl module should be used to read files with .xlsx extension.

main.py
import pandas as pd df = pd.read_excel( 'example.xlsx', engine='openpyxl' ) print(df)

pandas read excel with engine openpyxl

The code for this article is available on GitHub

On the other hand, the openpyxl module doesn't support files with .xls extension.

If you need to read a file with a .xls extension, use the xlrd module instead.

main.py
import pandas as pd df = pd.read_excel( 'another.xls', engine='xlrd' ) print(df)

read xls file using xlrd

Make sure the file you are trying to read hasn't been named using the wrong extension (.xlsx vs .xls).

# Conclusion

To solve the error error "xlrd.biffh.XLRDError: Excel xlsx file; not supported":

  1. Upgrade your version of the pandas module.
  2. Install the openpyxl module.
  3. Explicitly set the engine keyword argument to openpyxl in the call to the pandas.read_excel() method.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev