Open In App

Saving the Workbook using openpyxl

Last Updated : 23 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with Excel files in Python, the openpyxl library is a popular choice. It allows us to create, modify, and save Excel workbooks easily. In this article, we’ll go through how to create, update, and save a workbook using openpyxl, along with a small example project to demonstrate its functionality.

Prerequisites

Before we begin, install the openpyxl sheet using the following command:

pip install openpyxl

Understanding Workbook and Worksheet

In openpyxl, a workbook is like an entire Excel file, while a worksheet is like a single sheet within that workbook.

Working with a Workbook

Build a simple project that creates a new Excel workbook, adds some data to it, and then saves it.

  • Create an Excel workbook.
  • Add a sheet and populate it with some data.
  • Save the workbook with a specific filename.

1. Importing the Required Module

Python
from openpyxl import Workbook

2. Creating a New Workbook

Python
# Create a new workbook
wb = Workbook()

3. Accessing the Active Sheet

When we create a new workbook, it automatically comes with a default sheet named “Sheet”. We can access this sheet using Workbook.active property:

Python
# Access the active sheet
sheet = wb.active

# Optional: Rename the sheet
sheet.title = "DataSheet"

4. Adding Data to the Sheet

We can add data by specifying the cell and its value:

Python
# Adding some data to the sheet
sheet["A1"] = "Name"
sheet["B1"] = "Age"

# Inserting rows of data
sheet["A2"] = "POONAM"
sheet["B2"] = 28

sheet["A3"] = "MONA"
sheet["B3"] = 24

sheet["A4"] = "SITA"
sheet["B4"] = 30

5. Saving the Workbook

To save a work book, we can use the Workbook.save() method and pass the name.

Python
# Save the workbook
wb.save("example_project.xlsx")

Output:

Screenshot-2024-08-23-012148
Save openpyxl worksheet

After finishing all the above steps, we get an excel file named example_project.xlsx in the current working directory.

Explanation of the Complete Code

  1. We start by creating a new workbook and accessing the active worksheet.
  2. The sheet is optionally renamed to “DataSheet”.
  3. We then add headers and some sample data to the sheet.
  4. The .append() method is used to add rows of data efficiently.
  5. Finally, the workbook is saved with the filename example_project.xlsx.
Python
from openpyxl import Workbook

# Create a new workbook
wb = Workbook()

# Access the active sheet
sheet = wb.active

# Rename the sheet (optional)
sheet.title = "DataSheet"

# Adding headers to the sheet
sheet["A1"] = "Name"
sheet["B1"] = "Age"

# Inserting rows of data
data = [
    ["POONAM", 28],
    ["MONA", 24],
    ["SITA", 30]
]

for row in data:
    sheet.append(row)

# Save the workbook with a custom filename
wb.save("example_project.xlsx")

print("Workbook saved successfully!")

Running the Project

Run the Python script, and an Excel file named example_project.xlsx will be created in the current working directory. Open the file to see the data added.

Conclusion

In conclusion, working with Excel files in Python becomes seamless with the openpyxl library, which provides an efficient way to create, modify, and save Excel workbooks. This article has walked you through the basic steps of setting up a workbook, adding data, and saving it with openpyxl, demonstrated through a simple project. By following the provided steps and understanding how workbooks and worksheets function, we can easily manage Excel files programmatically. Whether it's for small tasks or larger projects, openpyxl is a valuable tool for handling Excel files within your Python applications.


Next Article
Article Tags :
Practice Tags :

Similar Reads