import excel with pandas using python

Viewed 20041

I'm new to Python and a have a problem. I'm trying to import an excel file with pandas and I'm using VS Code.

import pandas as pd

data = pd.read_excel(r'D:\Python\user.xlsx')
print(data)

I already installed xlrd, but however I get this error:

Traceback (most recent call last):
  File "d:\Python\import-excel.py", line 24, in <module>
    data = pd.read_excel(r'D:\Python\user.xlsx')
  File "D:\Python\venv\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "D:\Python\venv\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "D:\Python\venv\lib\site-packages\pandas\io\excel\_base.py", line 867, in __init__
    self._reader = self._engines[engine](self._io)
  File "D:\Python\venv\lib\site-packages\pandas\io\excel\_xlrd.py", line 22, in __init__
    super().__init__(filepath_or_buffer)
  File "D:\Python\venv\lib\site-packages\pandas\io\excel\_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "D:\Python\venv\lib\site-packages\pandas\io\excel\_xlrd.py", line 37, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "D:\Python\venv\lib\site-packages\xlrd\__init__.py", line 170, in open_workbook
    raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported

Can somebody help me?

1 Answers

As noted by other members, xlrd has discontinued support for formats other than .xls.

Recommended is to use another library such as openpyxl.

As suggested by user Chris Withers in his answer:

  1. Install openpyxl: pip install openpyxl

  2. Implement your code as:

    import pandas as pd
    
    data = pd.read_excel(r'D:\Python\user.xlsx', engine='openpyxl')
    
    print(data)
    

Hope this works for you!

Related