Python Xlsxwriter Create Excel File with Multiple Sheets

By Hardik Savani October 30, 2023 Category : Python

Hi Artisan,

In this comprehensive tutorial, we will learn python xlsxwriter create excel file with multiple sheets. This post will give you a simple example of xlsxwriter create excel file with multiple sheets. I explained simply about xlsxwriter write to multiple excel sheets. I explained simply about python xlsxwriter create excel file with multiple sheets. Alright, let’s dive into the steps.

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.

In this example, we will create excel file with SheetOne and SheetTwo multiple sheet. 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:

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 :
Shares