1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Jupyter notebook always return errors when loading an xlsx file

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm taking an online course, and I was trying to follow this code from the instructor. But I alway get an error message. This is the code I tried:

    import pandas as pd

    # Replace 'your_excel_file.xlsx' with the path to your Excel file
    excel_file = 'sales data.xlsx'

    # Load Excel data into a pandas DataFrame
    df = pd.read_excel(excel_file)

    # Now, you can work with the data in the 'df' DataFrame
    # For example, you can print the first few rows of the DataFrame
    df.head()


    Here's the error message. What can I do to fix this? --------------------------------------------------------------------------- ---------------------------------------------------------------------------

    ModuleNotFoundError Traceback (most recent call last)
    File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\compat\_optional.py:135, in import_optional_dependency(name, extra, errors, min_version)
    134 try:
    --> 135 module = importlib.import_module(name)
    136 except ImportError:

    File ~\AppData\Local\Programs\Python\Python313\Lib\importlib\__init__.py:88, in import_module(name, package)
    87 level += 1
    ---> 88 return _bootstrap._gcd_import(name[level:], package, level)

    File <frozen importlib._bootstrap>:1387, in _gcd_import(name, package, level)

    File <frozen importlib._bootstrap>:1360, in _find_and_load(name, import_)

    File <frozen importlib._bootstrap>:1324, in _find_and_load_unlocked(name, import_)

    ModuleNotFoundError: No module named 'openpyxl'

    During handling of the above exception, another exception occurred:

    ImportError Traceback (most recent call last)
    Cell In[2], line 7
    4 excel_file = 'sales data.xlsx'
    6 # Load Excel data into a pandas DataFrame
    ----> 7 df = pd.read_excel(excel_file)
    9 # Now, you can work with the data in the 'df' DataFrame
    10 # For example, you can print the first few rows of the DataFrame
    11 df.head()

    File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\excel\_base.py:495, in read_excel(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend, engine_kwargs)
    493 if not isinstance(io, ExcelFile):
    494 should_close = True
    --> 495 io = ExcelFile(
    496 io,
    497 storage_options=storage_options,
    498 engine=engine,
    499 engine_kwargs=engine_kwargs,
    500 )
    501 elif engine and engine != io.engine:
    502 raise ValueError(
    503 "Engine should not be specified when passing "
    504 "an ExcelFile - ExcelFile already has the engine set"
    505 )

    File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\excel\_base.py:1567, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options, engine_kwargs)
    1564 self.engine = engine
    1565 self.storage_options = storage_options
    -> 1567 self._reader = self._engines[engine](
    1568 self._io,
    1569 storage_options=storage_options,
    1570 engine_kwargs=engine_kwargs,
    1571 )

    File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\excel\_openpyxl.py:552, in OpenpyxlReader.__init__(self, filepath_or_buffer, storage_options, engine_kwargs)
    534 @doc(storage_options=_shared_docs["storage_options"])
    535 def __init__(
    536 self,
    (...) 539 engine_kwargs: dict | None = None,
    540 ) -> None:
    541 """
    542 Reader using openpyxl engine.
    543
    (...) 550 Arbitrary keyword arguments passed to excel engine.
    551 """
    --> 552 import_optional_dependency("openpyxl")
    553 super().__init__(
    554 filepath_or_buffer,
    555 storage_options=storage_options,
    556 engine_kwargs=engine_kwargs,
    557 )

    File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\compat\_optional.py:138, in import_optional_dependency(name, extra, errors, min_version)
    136 except ImportError:
    137 if errors == "raise":
    --> 138 raise ImportError(msg)
    139 return None
    141 # Handle submodules: if we have submodule, grab parent module from sys.modules

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

    Continue reading...

Compartilhe esta Página