In this tutorial, you will learn how to write data to an existing Excel file using Python.
There are several libraries available to read and write data from Excel. Here, we will use the openpyxl library which provides a Pythonic way to interact with Excel files.
Step 1: Installing openpyxl
The first step is to install the openpyxl library. You can do this using pip, which is the package installer for Python:
1 |
pip install openpyxl |
Step 2: Create an Excel file
First, we need to create a new file. You can do it manually or by running this simple code:
1 2 3 4 |
import openpyxl workbook = openpyxl.Workbook() workbook.save('codeigo.xlsx') |
Step 3: Opening the Excel File
Next, we need to open the Excel file. You can do this using the “load_workbook()” function in the openpyxl module:
1 2 3 4 |
from openpyxl import load_workbook # open the existing workbook workbook = load_workbook(filename='codeigo.xlsx') |
Here, codeigo.xslx is the name of the Excel file that you want to open. You can also provide the full path to the file if it is not in the same directory as your Python script.
Step 4: Selecting the Worksheet
Once you have opened the Excel file, you need to select the worksheet where you want to write data. You can do this using the “active” property of the workbook:
1 2 |
# get the active worksheet worksheet = workbook.active |
This will select the first worksheet in the Excel file. If you want to select a different worksheet, you can use the “worksheet” method and provide the name of the worksheet:
1 2 |
# select a specific worksheet worksheet = workbook['Sheet1'] |
Here, “Sheet1” is the name of the worksheet that you want to select.
Step 5: Writing Data to Excel
Now that you have selected the worksheet where you want to write data, you can start writing data in the Excel file. You can do this using the “cell()” method of the worksheet:
1 2 |
# write data to a cell worksheet['A1'] = 'Hello, World!' |
This will write the text “Hello, World!” to cell A1 in the worksheet. You can also write data to multiple cells at once using the “append()” method:
1 2 3 4 |
# write data to multiple cells worksheet.append(['Name', 'Age', 'Gender']) worksheet.append(['John Doe', 30, 'Male']) worksheet.append(['Jane Smith', 25, 'Female']) |
This will write the data to the next available row in the worksheet.
Step 6: Saving the Excel File
Once you have written data to the Excel file, you need to save the changes. You can do this using the “save()” method of the workbook:
1 2 |
# save the changes workbook.save(filename='file.xlsx') |
This will save the changes to the Excel file.
Example
Here is a complete example that demonstrates writing data to an existing Excel file using Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from openpyxl import load_workbook # open the existing workbook workbook = load_workbook(filename='codeigo.xlsx') # get the active worksheet worksheet = workbook.active # write data to a cell worksheet['A1'] = 'Hello, Codeigo!' # write data to multiple cells worksheet.append(['First Name', 'Last Name', 'Age', 'Gender']) worksheet.append(['John', 'Doe', 30, 'Male']) worksheet.append(['Jane', 'Smith', 25, 'Female']) # save the changes workbook.save(filename='codeigo.xlsx') |
This is what the example looks like:
Conclusion
In this tutorial, you learned how to write data to an existing Excel file using Python and the openpyxl library. You can now use this knowledge to customize and automate your data analysis workflows.