问题:

使用pandas包,没有使用openpyxl包,但是报错:ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

翻译:缺少可选择的依赖项“openpyxl”,使用 pip install openpyxl or conda install openpyxl

解决方法:

  • 首先,激活你的项目环境:activate “name of your project”
  • 然后,安装openpyxl包:pip install openpyxl
  • 注:直接安装到conda环境下:conda install openpyxl

此时,已经解决了遇到的问题,但是为什么呢,我们一起来分析pandas中的部分涉及openpyxl的文件的源码:


(1)pandas中的_openpyxl.py文件

类 OpenpyxlReader,说明了 pandas 在读取 xls 文件时,使用 openpyxl 引擎的阅读器(Reader using openpyxl engine)。

(2)pandas中的_optional.py文件

导入一个可以选择的依赖项。默认情况下,如果依赖项丢失,将引发带有好消息的 ImportError。 如果存在依赖项,但太旧,我们会提出。

def import_optional_dependency(
    name: str,
    extra: str = "",
    errors: str = "raise",
    min_version: str | None = None,
):
    """
    Import an optional dependency.

    By default, if a dependency is missing an ImportError with a nice
    message will be raised. If a dependency is present, but too old,
    we raise.

    Parameters
    ----------
    name : str
        The module name.
    extra : str
        Additional text to include in the ImportError message.
    errors : str {'raise', 'warn', 'ignore'}
        What to do when a dependency is not found or its version is too old.

        * raise : Raise an ImportError
        * warn : Only applicable when a module's version is to old.
          Warns that the version is too old and returns None
        * ignore: If the module is not installed, return None, otherwise,
          return the module, even if the version is too old.
          It's expected that users validate the version locally when
          using ``errors="ignore"`` (see. ``io/html.py``)
    min_version : str, default None
        Specify a minimum version that is different from the global pandas
        minimum version required.
    Returns
    -------
    maybe_module : Optional[ModuleType]
        The imported module, when found and the version is correct.
        None is returned when the package is not found and `errors`
        is False, or when the package's version is too old and `errors`
        is ``'warn'``.
    """

    assert errors in {"warn", "raise", "ignore"}

    package_name = INSTALL_MAPPING.get(name)
    install_name = package_name if package_name is not None else name

    msg = (
        f"Missing optional dependency '{install_name}'. {extra} "
        f"Use pip or conda to install {install_name}."
    )
    try:
        module = importlib.import_module(name)
    except ImportError:
        if errors == "raise":
            raise ImportError(msg) from None
        else:
            return None

    # Handle submodules: if we have submodule, grab parent module from sys.modules
    parent = name.split(".")[0]
    if parent != name:
        install_name = parent
        module_to_get = sys.modules[install_name]
    else:
        module_to_get = module
    minimum_version = min_version if min_version is not None else VERSIONS.get(parent)
    if minimum_version:
        version = get_version(module_to_get)
        if Version(version) < Version(minimum_version):
            msg = (
                f"Pandas requires version '{minimum_version}' or newer of '{parent}' "
                f"(version '{version}' currently installed)."
            )
            if errors == "warn":
                warnings.warn(msg, UserWarning)
                return None
            elif errors == "raise":
                raise ImportError(msg)

    return module

其中,报错的信息来自下面这四行代码,显示我们没有可选择的依赖项“openpyxl”,下载即可。

msg = (
        f"Missing optional dependency '{install_name}'. {extra} "
        f"Use pip or conda to install {install_name}."
    )

感悟:利用 python 对 Excal 表格进行增删查改对办公效率的提高有很大的帮助,本人习惯利用 pandas 和 xlswriter 对表格的读取、修改和保存。

综合运用pandas和xlsxwriter解决所需问题(读取表格、更改数据、保存到新表格),附加一些注意事项_Flying Bulldog的博客-CSDN博客_pandas xlsxwriter(6)注意事项:pandas读取xlsx文件时,可能出现错误,这是需要另存为xls文件当worksheet.close()报错时,大概率不是此处的错误,可能是路径的问题写好内容格式后,一定要记得添加格式在字符串的指定位置插入内容,这个思想很重要,具体实现见上述代码https://blog.csdn.net/qq_54185421/article/details/124239391  >>>如有疑问,欢迎评论区一起探讨。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐