首页 > 解决方案 > 即使安装了“openpyxl”也无法读取 xlsx 文件

问题描述

我想将 .xls 和 .xlsx 文件都读取到数据帧中,根据互联网上的内容,我的系统中应该安装包 ['xlrd' 和 'openpyxl']

我的系统是MAC,独立安装了[python和Jupyter lab],也安装了[Anaconda],我可以从终端或Anaconda浏览器运行Jupyter Lan。

安装“xlrd”后,我可以毫无问题地读取 .xls 文件。

但在安装“openpyxl”并尝试读取 .xlsx 文件后,出现以下错误:

我试图卸载并重新安装 openpyxl 但仍然存在同样的问题。

同时我检查了openpyxl的两个安装,在Anaconda它是v3.0.5,在python它是v3.0.6

这是正常的吗??

 ---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-11-f15b3a520ce0> in <module>
      1 ### 1.2 : Load Sciamgo Jurnal Excel file with xlsx format
----> 2 xlsx = pd.ExcelFile('assets/scimagojr-3.xlsx', engine='openpyxl')
      3 sciamgo = pd.read_excel(xlsx)
      4 
      5 

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/excel/_base.py in __init__(self, io, engine)
    822         self._io = stringify_path(io)
    823 
--> 824         self._reader = self._engines[engine](self._io)
    825 
    826     def __fspath__(self):

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/io/excel/_openpyxl.py in __init__(self, filepath_or_buffer)
    482             Object to be parsed.
    483         """
--> 484         import_optional_dependency("openpyxl")
    485         super().__init__(filepath_or_buffer)
    486 

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/compat/_optional.py in import_optional_dependency(name, extra, raise_on_missing, on_version)
     90     except ImportError:
     91         if raise_on_missing:
---> 92             raise ImportError(msg) from None
     93         else:
     94             return None

ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

标签: pythonpandasopenpyxl

解决方案


推荐阅读