Last modified: Mar 28, 2025 By Alexander Williams

Install Openpyxl in Python Step by Step

Openpyxl is a Python library for working with Excel files. It allows you to read, write, and modify Excel files easily. This guide will show you how to install it.

Prerequisites

Before installing Openpyxl, ensure you have Python installed. You can check by running python --version in your terminal.


python --version


Python 3.9.7

If Python is not installed, download it from the official website. Also, ensure pip is available. Pip is Python's package installer.

Install Openpyxl Using Pip

The easiest way to install Openpyxl is using pip. Open your terminal or command prompt and run the following command.


pip install openpyxl


Collecting openpyxl
  Downloading openpyxl-3.0.10-py2.py3-none-any.whl (242 kB)
Successfully installed openpyxl-3.0.10

This command downloads and installs Openpyxl and its dependencies. If you face issues, check our guide on How To Solve ModuleNotFoundError.

Verify the Installation

After installation, verify Openpyxl is installed correctly. Run the following Python code in your IDE or terminal.


import openpyxl
print(openpyxl.__version__)


3.0.10

If you see the version number, Openpyxl is installed correctly. If not, recheck the installation steps.

Install Openpyxl in a Virtual Environment

Using a virtual environment is recommended for Python projects. It keeps dependencies isolated. Follow these steps to install Openpyxl in a virtual environment.

First, create a virtual environment.


python -m venv myenv

Activate the virtual environment.


# On Windows
myenv\Scripts\activate

# On macOS/Linux
source myenv/bin/activate

Now, install Openpyxl inside the virtual environment.


pip install openpyxl

This ensures Openpyxl is only available in this project. It avoids conflicts with other projects.

Basic Usage of Openpyxl

Once installed, you can start using Openpyxl. Here’s a simple example to create an Excel file.


from openpyxl import Workbook

# Create a new workbook
wb = Workbook()

# Get the active worksheet
ws = wb.active

# Add data to the worksheet
ws['A1'] = "Hello"
ws['B1'] = "World"

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

This code creates an Excel file named example.xlsx with "Hello" in cell A1 and "World" in cell B1.

Common Issues and Solutions

Sometimes, you may encounter issues during installation. Here are some common problems and solutions.

1. ModuleNotFoundError: If you see this error, Openpyxl is not installed. Reinstall it using pip.

2. Permission Issues: On some systems, you may need admin rights. Use sudo pip install openpyxl on macOS/Linux.

3. Outdated Pip: Ensure pip is updated. Run pip install --upgrade pip before installing Openpyxl.

For more details, refer to our guide on How To Solve ModuleNotFoundError.

Conclusion

Installing Openpyxl in Python is simple with pip. Follow the steps above to get started. Openpyxl is a powerful tool for working with Excel files in Python.

Now you can read, write, and modify Excel files easily. Happy coding!