How to Open an Excel File using Python OpenPyxl

Python is a powerful programming language with vast capabilities. Among them, it offers excellent tools for working with Microsoft Excel files. In this tutorial, we’re going to look at how to open an Excel file using a Python library called OpenPyxl.

OpenPyxl allows you to read and write Excel files, without requiring the Excel software. It’s particularly helpful when dealing with large datasets or complex computations.

Step 1: Create an Excel file

Create an Excel file (sample.xlsx) with content:

Step 2: Install OpenPyxl

The first thing you need to do is install the OpenPyxl library if you haven’t already. To install OpenPyxl, open your terminal or command prompt and execute the following command:

Step 3: Import OpenPyxl

Once OpenPyxl is installed, you need to import it into your Python script. Here is how you can do it:

Step 4: Load the Workbook

To open an Excel file with OpenPyxl, you need to load it into a Workbook object. Let’s assume we have an Excel file named “sample.xlsx”. Here is how you can open it:

In the above code, ‘wb’ is a variable that holds the Workbook object. ‘sample.xlsx’ is the name of your Excel file.

Step 5: Access a Worksheet

The Excel file (Workbook) may contain multiple sheets. To work with the data, you need to access a specific worksheet. Here is how you can do it:

In the above code, ‘sheet’ is a variable that holds the Worksheet object. ‘Sheet1’ is the name of the sheet in your Excel file.

Step 6: Work with the Data

Now that we can access a Worksheet object, we can start working with the data. For instance, we can read the value in cell A1 as follows:

In conclusion, opening an Excel file with Python and OpenPyxl is simple and straightforward. It’s an invaluable skill when dealing with large datasets or performing complex computations.

Full Code

Output

Name

Conclusion

By following these simple steps, anyone can easily access and manipulate data in Excel files using Python and OpenPyxl. This makes it a valuable tool for data analysis, automation, and many other tasks.