Saving the Workbook using openpyxl
Last Updated :
23 Aug, 2024
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:
Save openpyxl worksheetAfter finishing all the above steps, we get an excel file named example_project.xlsx in the current working directory.
Explanation of the Complete Code
- We start by creating a new workbook and accessing the active worksheet.
- The sheet is optionally renamed to “DataSheet”.
- We then add headers and some sample data to the sheet.
- The .append() method is used to add rows of data efficiently.
- 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.
Similar Reads
Creating the Workbook and Worksheet using openpyxl in Python Openpyxl is a Python library designed to read and write Excel (xlsx/xlsm/xltx/xltm) files. It's a great tool for automating Excel operations in Python, making it easier to create, modify, and extract information from Excel files.Openpyxl is a Python library that lets us work with Excel files directl
6 min read
Excel Personal Macro Workbook When we create the macros for automation, they can be used for that particular workbook only. We cannot access them outside that workbook in others. Excel Personal Macro Workbook is a hidden workbook in your local system that pops up whenever you open the Excel application. It is a place where all t
4 min read
Workbooks in Microsoft Excel Spreadsheet programs have become essential for many organizations for analyzing and storing data and if we talk about the most recognized spreadsheet program, then it will be MS Excel. Now, if you have worked with Microsoft Excel, then you have heard about the terms workbooks and worksheets. In the
5 min read
How to Protect a Workbook in MS Excel? Every day in school, offices, business sectors or any other field lots of information are there that are required to store for the future use. For anyone, it is very difficult to remember that information for a long time. Earlier data and information are stored in a form of a register, file, or pape
4 min read
How to replace a word in excel using Python? Excel is a very useful tool where we can have the data in the format of rows and columns. We can say that before the database comes into existence, excel played an important role in the storage of data. Nowadays using Excel input, many batch processing is getting done. There may be the requirement o
3 min read