ItSolutionStuff.com

Python Xlsxwriter Create Excel File Example

By Hardik Savani • October 30, 2023
Python

Hey Dev,

This post will give you an example of python xlsxwriter create excel file example. we will help you to give an example of how to create excel file in python xlsxwriter. Here you will learn how to create excel file from dataframe in python. I would like to show you how to create xlsx file in python xlsxwriter.

Xlsxwriter is a Python module that is used to write to an Excel file. Xlsxwriter is used to write text, numbers, formulas and hyperlinks to multiple worksheets etc. We can work with Workbook using Python Xlsxwriter.

I will give you simple two examples of creating an excel file using xlsxwriter. 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 xlsxwriter in your system then you can install using the below command:

pip install xlsxwriter

Example 1:

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

Example 2: Multiple Sheet

main.py

import xlsxwriter
  
# Cretae a xlsx file
xlsxFile = xlsxwriter.Workbook('demo.xlsx')
  
# Add new worksheet
sheetOne = xlsxFile.add_worksheet("SheetOne")
sheetTwo = xlsxFile.add_worksheet("SheetTwo")
  
# 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(SheetONE)
sheetOne.write(0, 0, "ID")
sheetOne.write(0, 1, "Name")
sheetOne.write(0, 2, "Email")
  
# Set Header for xlsx file(SheetTwo)
sheetTwo.write(0, 0, "ID")
sheetTwo.write(0, 1, "Name")
sheetTwo.write(0, 2, "Email")
  
# write into the worksheet
for item in data :
   
    # write operation perform(SheetOne)
    sheetOne.write(row, 0, item["ID"])
    sheetOne.write(row, 1, item["Name"])
    sheetOne.write(row, 2, item["Email"])
  
    # write operation perform(SheetTwo)
    sheetTwo.write(row, 0, item["ID"])
    sheetTwo.write(row, 1, item["Name"])
    sheetTwo.write(row, 2, item["Email"])
   
    # incrementing the value of row by one
    row += 1
  
# Close the Excel file
xlsxFile.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: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Openpyxl Create Excel File Example

Read Now →

Python Pandas Create Excel File with Multiple Sheets

Read Now →

Python Pandas Create Excel File Example

Read Now →

How to Create and Write Xlsx File in Python?

Read Now →

How to Create Excel File in Python?

Read Now →

Python Create JSON File from List Example

Read Now →

How to Create a JSON File in Python?

Read Now →

Python Read CSV File Line by Line Example

Read Now →

Python Generate Text File from List Example

Read Now →

Python Create Text File in Specific Directory Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

Python PATCH Request with Parameters Example

Read Now →