openpyxl

raw JSON →
3.1.5 verified Tue May 12 auth: no python install: verified quickstart: verified

A Python library to read/write Excel 2010 xlsx/xlsm files. Current version: 3.1.5. Openpyxl is actively maintained with regular updates to support the latest Excel features and Python versions.

pip install openpyxl
error ModuleNotFoundError: No module named 'openpyxl'
cause The openpyxl library is not installed in the Python environment being used, or there is a mismatch between the Python version where openpyxl was installed and the Python interpreter running the code.
fix
Install openpyxl using pip: pip install openpyxl or python -m pip install openpyxl. If using virtual environments, ensure it's installed in the active environment.
error AttributeError: 'Workbook' object has no attribute 'get_sheet_by_name'
cause The `get_sheet_by_name()` method was deprecated in openpyxl 2.4 and removed in later versions.
fix
Access worksheets directly using dictionary-like syntax: worksheet = workbook['SheetName'] or iterate through workbook.sheetnames.
error FileNotFoundError: [Errno 2] No such file or directory: 'your_file.xlsx'
cause The specified Excel file does not exist at the given path, or the path is incorrect relative to the script's working directory.
fix
Verify the file name and its full path. Ensure the file is in the same directory as the script, or provide an absolute path to the file.
error AttributeError: 'NoneType' object has no attribute 'value'
cause This typically occurs when trying to access the 'value' attribute of a cell that is empty or a variable that was expected to hold a cell object but instead received `None`, often because a cell was out of range or a worksheet was not properly loaded.
fix
Before accessing .value, check if the cell object is not None. For example, if cell is not None: print(cell.value). Ensure correct cell referencing and that the workbook/worksheet is loaded correctly.
error ValueError: Invalid file path or buffer object type: <class 'openpyxl.workbook.workbook.Workbook'>
cause This error often occurs when attempting to pass an `openpyxl.workbook.Workbook` object to a function (e.g., `pandas.read_excel`) that expects a file path, file-like object, or buffer, not an already-loaded `openpyxl` workbook object.
fix
Pass the file path directly to the function expecting it, or if you must use a loaded openpyxl workbook, save it to a buffer first if the target function supports buffer input. For pandas.read_excel, either provide the file path directly or ensure the 'engine' parameter is set correctly if passing a file-like object.
breaking Openpyxl does not guard against XML-based attacks like quadratic blowup or billion laughs attacks by default. To mitigate these risks, install the 'defusedxml' package. ([openpyxl.readthedocs.io](https://openpyxl.readthedocs.io/en/3.1.0/?utm_source=openai))
fix Install 'defusedxml' using pip: pip install defusedxml
gotcha Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead. Also, a new release of pip is available.
fix Use a virtual environment for pip operations or explicitly manage --root-user-action. To update pip, run: pip install --upgrade pip
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.31s 20.5M
3.10 slim (glibc) - - 0.21s 21M
3.11 alpine (musl) - - 0.41s 22.7M
3.11 slim (glibc) - - 0.34s 23M
3.12 alpine (musl) - - 0.35s 14.5M
3.12 slim (glibc) - - 0.34s 15M
3.13 alpine (musl) - - 0.34s 14.1M
3.13 slim (glibc) - - 0.38s 15M
3.9 alpine (musl) - - 0.31s 20.0M
3.9 slim (glibc) - - 0.28s 20M

A simple example to create a new Excel file and write data to it.

from openpyxl import Workbook

# Create a new workbook and select the active worksheet
wb = Workbook()
ws = wb.active

# Write data to a cell
ws['A1'] = 'Hello, World!'

# Save the workbook
wb.save('example.xlsx')