Create and Write on Excel File using xlsxwriter Module Python (original) (raw)

Last Updated : 07 Apr, 2025

**XlsxWriter The xlsxwriter module in Python is used to create Excel .xlsx files and write data into them. It supports:

Installation

Before using **xlsxwriter, we need to install it using pip:

pip install xlsxwriter

**Note:

Rows and columns are zero-indexed. For example:

Examples of xlsxwriter module

Example 1: Writing to Excel Using A1 Notation

In this example, we'll create a new Excel file and write simple text values to specific cells using the familiar A1-style cell notation:

Python `

import xlsxwriter

workbook = xlsxwriter.Workbook('path_to_hello.xlsx') worksheet = workbook.add_worksheet()

worksheet.write('A1', 'Hello..') worksheet.write('B1', 'Geeks') worksheet.write('C1', 'For') worksheet.write('D1', 'Geeks')

workbook.close()

`

**Output:

**Explanation:

This creates a single worksheet with text in the first row from columns A to D.

Example 2: Writing Using Row-Column Indexing

This example shows how to write a list of names to the first column using zero-based indexing (row, column) instead of A1-style cell notation:

Python `

import xlsxwriter

workbook = xlsxwriter.Workbook('example2.xlsx') worksheet = workbook.add_worksheet()

content = ["ankit", "rahul", "priya", "harshita", "sumit", "neeraj", "shivam"]

for row, name in enumerate(content): worksheet.write(row, 0, name)

workbook.close()

`

**Output:

**Explanation:

Example 3: Creating a Named Worksheet and Writing Rows

Here, we create a custom-named worksheet and write a **2D list of names and scores into it **row-by-row:

Python `

import xlsxwriter

workbook = xlsxwriter.Workbook('example3.xlsx') worksheet = workbook.add_worksheet("My Sheet")

scores = [ ['ankit', 1000], ['rahul', 100], ['priya', 300], ['harshita', 50], ]

for row, (name, score) in enumerate(scores): worksheet.write(row, 0, name) worksheet.write(row, 1, score)

workbook.close()

`

**Output:

output3

**Explanation:

  1. The worksheet is named "My Sheet" instead of the default "Sheet1".
  2. We write both names and their corresponding scores into two columns.
    • Column A - Names
    • Column B - Scores
  3. The data is written row-by-row using **enumerate().

Advantages of xlsxwriter

Limitations