Change language

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically