How to Create Excel File in Python?

By Hardik Savani October 30, 2023 Category : Python

Hello Dev,

I am going to explain to you example of how to create excel file in python. It's a simple example of python write excel file example. I explained simply about python create excel file if not exists. This example will help you how to create excel file in python. So, let us see in detail an example.

There are several modules and ways to create excel files in python. i will give you simple three examples using pandas, openpyxl and xlsxwriter to generate excel file data. so let's see examples one by one.

You can use these examples with python3 (Python 3) version.

Example 1: using pandas

If you haven't install pandas in your system then you can install using the below command:

pip install pandas

main.py

import pandas as pd
  
# Create DataFrame for XLSX File
df = pd.DataFrame({'ID': [1, 2, 3],
                   'Name': ["Hardik Savani", "Vimal Kashiyani", "Harshad Pathak"],
                   'Email': ["hardik@gmail.com", "vimal@gmail.com", "harshad@gmail.com"]})

# Create a Pandas Excel writer using XlsxWriter
writer = pd.ExcelWriter('demo.xlsx', engine='xlsxwriter')
  
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1', index=False)

# Save Data to File
writer.save()

Output:

Now, It will generated demo.xlsx file in your root path with the below content.

demo.xlsx

Example 2: using openpyxl

If you haven't install openpyxl in your system then you can install using the below command:

pip install openpyxl

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 3: using xlsxwriter

If you haven't install xlsxwriter in your system then you can install using the below command:

pip install xlsxwriter

main.py

import xlsxwriter
  
# Cretae a xlsx file
xlsx_File = xlsxwriter.Workbook('demo.xlsx')
  
# Add new worksheet
sheet_schedule = xlsx_File.add_worksheet()
  
# Create List for write data into xlsx file
data = [
    { "ID": 1, "Name": "Hardik Savani", "Email": "hardik@gmail.com"},
    { "ID": 2, "Name": "Vimal Kashiyani", "Email": "vimal@gmail.com"},
    { "ID": 3, "Name": "Harshad Pathak", "Email": "harshad@gmail.com"}
]
  
row = 1
column = 0
  
# Set Header for xlsx file
sheet_schedule.write(0, 0, "ID")
sheet_schedule.write(0, 1, "Name")
sheet_schedule.write(0, 2, "Email")
  
# write into the worksheet
for item in data :
   
    # write operation perform
    sheet_schedule.write(row, 0, item["ID"])
    sheet_schedule.write(row, 1, item["Name"])
    sheet_schedule.write(row, 2, item["Email"])
   
    # incrementing the value of row by one
    row += 1
  
# Close the Excel file
xlsx_File.close()

Output:

Now, It will generated demo.xlsx file in your root path with the below content.

demo.xlsx

I hope it can help you...

Tags :
Shares