Python Openpyxl Create Excel File Example
Hello Developer,
In this tutorial, you will discover python openpyxl create excel file example. We will use how to create excel file in python openpyxl. We will use how to create excel file from dataframe in python. This article goes in detailed on how to create xlsx file in python openpyxl. So, let's follow a few steps to create an example of openpyxl create excel file if not exists.
Openpyxl is a Python library that is used to read from an xlsx file or write to an Excel file. Openpyxl is used to analyze data, data copying, data mining etc. We can work with Workbook using Python Openpyxl.
I will give you simple two examples of creating an excel file using openpyxl. in the first example, we will create a simple xlsx file with one sheet. in the second example, we will create xlsx file with multiple sheets. so let's see the below examples.
You can use these examples with python3 (Python 3) version.
If you haven't install openpyxl in your system then you can install using the below command:
pip install openpyxl
Example 1:
main.py
import openpyxl # Define variable to load the dataframe wb = openpyxl.Workbook() # Define active sheet sheet = wb.active # Create List for store data data =[('ID', 'Name', 'Email'), (1, 'Hardik Savani', 'hardik@gmail.com'), (2, 'Vimal Kashiyani', 'vimal@gmail.com'), (3, 'Harshad Pathak', 'harshad@gmail.com')] # Adding Data to Sheet for item in data : sheet.append(item) # Save File wb.save("demo.xlsx")
Output:
Now, It will generated demo.xlsx file in your root path with the below content.
demo.xlsx
Example 2: Multiple Sheet
main.py
import openpyxl # Define variable to load the dataframe wb = openpyxl.Workbook() # Create SheetOne with Data sheetOne = wb.create_sheet("SheetOne") data =[('ID', 'Name', 'Email'), (1, 'Hardik Savani', 'hardik@gmail.com'), (2, 'Vimal Kashiyani', 'vimal@gmail.com'), (3, 'Harshad Pathak', 'harshad@gmail.com')] for item in data : sheetOne.append(item) # Create SheetTwo with Data sheetTwo = wb.create_sheet("SheetTwo") data =[('ID', 'Name'), (1, 'Hardik'), (2, 'Vimal'), (3, 'Harshad')] for item in data : sheetTwo.append(item) # Remove default Sheet wb.remove(wb['Sheet']) # Iterate the loop to read the cell values wb.save("demo.xlsx")
Output:
Now, It will generated demo.xlsx file in your root path with the below content.
demo.xlsx
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Pandas Create Excel File Example
- How to Create and Write Xlsx File in Python?
- How to Create Excel File in Python?
- Python Read Excel File using Pandas Example
- How to Open and Read Xlsx File in Python?
- How to Read Excel File in Python?
- How to Read a JSON File in Python?
- Python Read CSV File Line by Line Example
- Python Read CSV File Without Header Example
- Python Read Text File into List Example
- Python Create Zip Archive from Directory Example