You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pandas导入Excel文件失败,求助解决方法

解决Pandas读取Excel时的openpyxl缺失错误

问题代码

import pandas as pd

df = pd.read_excel('Sales.xlsx')

print(df)

错误信息

Traceback (most recent call last):
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\compat\_optional.py", line 141, in import_optional_dependency
    module = importlib.import_module(name)
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'openpyxl'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wongs\OneDrive\Desktop\utar\y2s3\data mining\P2 TASK 3.py", line 3, in <module>
    df = pd.read_excel('Sales.xlsx')
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py", line 482, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py", line 1695, in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 556, in __init__
    import_optional_dependency("openpyxl")
  File "C:\Users\wongs\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\compat\_optional.py", line 144, in import_optional_dependency
    raise ImportError(msg)
ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

问题原因

Pandas读取.xlsx格式文件时,默认依赖openpyxl库作为解析引擎,当前Python环境未安装该库,导致执行报错。

解决方案

根据你的包管理工具,选择以下命令安装openpyxl:

使用pip安装

打开终端或命令提示符,执行:

pip install openpyxl

若系统中同时存在Python2和Python3,可指定使用pip3:

pip3 install openpyxl

使用conda安装(适用于Anaconda/Miniconda环境)

conda install openpyxl

安装完成后,重新运行代码即可正常读取Excel文件。

内容的提问来源于stack exchange,提问作者YEW KANG WONG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:35:20