How To Use Python On Excel

In this tutorial, we will learn how to use Python on Excel spreadsheets using the openpyxl library in Python. Python is a powerful programming language that is well-suited for spreadsheet processing, and the openpyxl library provides an easy and clear way to work with Excel documents.

To get started, you should have Python and pip (the package installer for Python) installed on your computer.

Step 1: Install the openpyxl library

To install the openpyxl library, open your terminal or Command Prompt and run the following command:

This will download and install the library on your system.

Step 2: Create a sample Excel file

Before we begin working with Python, let’s create a sample Excel file with some data. In this example, we will use a file named **sample_data.xlsx** with the following data:

Name  Age  Country
Alice 25   USA
Bob   30   Canada
Eve   28   France
Steve 22   UK
Sara  27   Germany

Save this file to a location of your choice on your computer.

Step 3: Read and display data from the Excel file

Now that we have the sample Excel file, let’s read and display its data using openpyxl. Use the following code to do so:

This code will display the following output:

('Name', 'Age', 'Country')
('Alice', 25, 'USA')
('Bob', 30, 'Canada')
('Eve', 28, 'France')
('Steve', 22, 'UK')
('Sara', 27, 'Germany')

Step 4: Modify data in the Excel file

Now let’s update the age value of Alice from 25 to 26 using the following code:

This code will create a new Excel file named **sample_data_modified.xlsx** with the updated age value of Alice:

Name  Age  Country
Alice 26   USA
Bob   30   Canada
Eve   28   France
Steve 22   UK
Sara  27   Germany

Full Code

Conclusion

In this tutorial, we have learned **how to use Python with Excel** by seeing an example that demonstrates reading, displaying, and modifying data in Excel files using the **openpyxl** library.

This is just the beginning; there are many other features and functionalities provided by openpyxl which can assist you in working with Excel using Python. For more information, refer to the [official documentation](https://openpyxl.readthedocs.io/en/stable/) of the openpyxl library.