Python | Create and write to an Excel file using the xlsxwriter module

Use this command to install the xlsxwriter module:

 pip install xlsxwriter 

Note. In XlsxWriter, rows and columns are zero-indexed. The first cell on the sheet, A1 — this is (0, 0), B1 — (0, 1), A2 — (1, 0), B2 — (1, 1) & # 8230; Likewise for everyone.

Let`s see how to create and write an Excel sheet using Python.

Code # 1: Using the A1 record (cell name ) to write data to specific cells.

# xlsxwriter module import

import xlsxwriter

 
# Workbook () takes one optional argument
# this is the name of the file we want to create.

workbook = xlsxwriter.Workbook ( ` hello.xlsx` )

 
# Then the book object is used to add a new
# worksheet using the add_worksheet () method.

worksheet = workbook.add_worksheet ()

 
# Use the sheet object to write
# data via the write () method.

worksheet.write ( `A1` , ` Hello..` )

worksheet.write ( `B1` , ` Geeks` )

worksheet.write ( `C1` , `For` )

worksheet.write ( `D 1` , `Geeks` )

 
# Finally, close the Excel file
# using the close () method.
workbook.close ()

Exit:

Code # 2: Using a row-column entry (indexing value) to write data to specific cells.

# import xlsxwriter module

import xlsxwriter

  

workbook = xlsxwriter.Workbook ( `Example2 .xlsx` )

worksheet = workbook.add_worksheet ()

 
# Start at the first cell.
# Rows and columns are zero-indexed.

row = 0

column = 0

 

content = [ "ankit" , "rahul" , "priya" < code class = "plain">, "harshita" ,

"sumit" , "neeraj" , "shivam" ]

 
# view content list

for item in content:

 

# perform write operation

worksheet.write (row, column, item)

 

# increment the string value by one tsu

# from each iteration.

row + = 1

 
workbook.close ( )

Output:

Code # 3: Create a new sheet with a specific name

# xlsxwriter module import

import xlsxwriter

 

workbook  = xlsxwriter.Workbook ( `Example3.xlsx` )

 
# By default, worker names the sheets in the spreadsheet will be
# Sheet1, Sheet2, etc., but we can also provide a name.

worksheet = workbook.add_worksheet ( " My sheet " )

  
# Some data we want to write to the sheet.

scores = (

[ `ankit` , 1000 ],

[ `rahul` 100 ],

[ `priya` 300 ],

[ `harshita` 50 ],

)

 
# Start at the first cell. Rows and
# columns are indexed with zeros.

row = 0

col = 0

 
# Loop through the data and write it line by line.

for name, score in (scores):

worksheet.write (row, col, name)

worksheet .write (row, col + 1 , score)

row + =   1

 
workbook.close ()

Output:

XlsxWriter has some advantages and disadvantages over the alternative Python modules for writing Excel files.

Benefits :

  • It supports more Excel functions than any of the alternative modules.
  • It has a high degree of fidelity with files generated by in Excel. In most cases, the files generated are 100% equivalent to files created in Excel.
  • Has extensive documentation, sample files and tests.
  • It`s fast and can be configured to use very little memory. even for very large output files.

Disadvantages :

  • It cannot read or modify existing Excel XLSX files.