11

I want to open an Excel file in Python, using:

import xlrd

loc = (r"C:\Users\my_path\my_file.xlsx")

wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)

and it caught error:

---------------------------------------------------------------------------
XLRDError                                 Traceback (most recent call last)
<ipython-input-70-b399ced4986e> in <module>
      4 loc = (r"C:\Users\my_path\my_file.xlsx")
      5 
----> 6 wb = xlrd.open_workbook(loc)
      7 sheet = wb.sheet_by_index(0)
      8 sheet.cell_value(0, 0)

C:\Python38\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows, ignore_workbook_corruption)
    168     # files that xlrd can parse don't start with the expected signature.
    169     if file_format and file_format != 'xls':
--> 170         raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
    171 
    172     bk = open_workbook_xls(

XLRDError: Excel xlsx file; not supported

What is wrong?

5
  • Do you have the right filename? It looks like you're trying to open a directory, rather than a specific Excel file within it; it would normally have an .xls or .xlsx extension... (or .xlsm or .xlsb etc). Commented May 5, 2021 at 3:16
  • Hi @sabik thank you for pointing out, please see the edited question. Commented May 5, 2021 at 3:25
  • 1
    Well now the error message tells you exactly what's wrong: "Excel xlsx file; not supported". Per the homepage of the xlrd library, it can only read "Excel files in the historical .xls format." Commented May 5, 2021 at 3:28
  • 3
    pass engine="openpyxl" to pd.read_excel.' Commented May 5, 2021 at 3:33
  • Hi @QuangHoang Thanks pd.read_excel(r'C:\Users\my_path\my_file.xlsx', engine="openpyxl") worked ! Commented May 5, 2021 at 3:41

2 Answers 2

21

The lastest version of xlrd is only support .xls file, so you can install the older version

pip uninstall xlrd

pip install xlrd==1.2.0
Sign up to request clarification or add additional context in comments.

Comments

8

Or you can use the openpyxl to do it.

pip install openpyxl

If you use it in Pandas:

import pandas as pd
pd.read_excel('file/path/to/excel/spreadsheet.xlsx', engine='openpyxl')

If you don't specify the engine, it will use whatever default.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.