Writing to an excel sheet using “xlwt” module in Python 3.x or earlier

In this tutorial, we will be learning about the writing operations that can be performed on the excel file in Python. Here we will use the inbuilt module “xlwt” in Python 3.x. or earlier. Writing excel file in Python includes these following operations:
  • Create an excel file and values in it.
  • Change the format of elements. ( change font size, font type etc ).
  • Add multiple style parameters at a time.

Read my previous tutorial to learn Reading an excel sheet using xlrd module in Python

Write Excel File in Python – Create, Insert elements, Change format

In the xlwt module, User can perform multiple operations on the spreadsheet. Here writing or modifying the data can be done in Python 3.x. or earlier. User can go through various sheets. He/She can extract data based on some constraints or modify some rows or columns.

Installation of xlwt module

With the help of pip, we can install the module very easily.
For Windows User: Command Line run
For Linux User: Terminal run

pip install xlwt

Writing on an excel file

# Writing to an excel sheet using Python 3.x. or earlier 
import xlwt as xw
  
# Workbook is created 
wb = xw.Workbook() 
  
# add_sheet is used to create sheet. 
sheet1 = wb.add_sheet('Sheet 1') 
 
# Input data into rows 
sheet1.write(1, 0, 'Code Speedy') 
sheet1.write(2, 0, 'Sarque Ahamed Mollick') 
sheet1.write(3, 0, 'Farque Ahamed Mollick') 
sheet1.write(4, 0, 'Pavitra Walia') 

# Input data into columns
sheet1.write(0, 1, 'Position') 
sheet1.write(0, 2, 'No of Posts') 
sheet1.write(0, 3, 'Cornerstone content') 
sheet1.write(0, 4, 'Catagory') 

  
wb.save('xlwt codespeedy.xls')
Output: An excel sheet gets created with the data that we specified above.

Here write function takes three attributes row_index, col_index, cell_value.
Finally, with the help of .save() function, we can save our spreadsheet.

Changing the style of elements(font size, font type,…)

# importing xlwt module 
import xlwt as xw
  
wb= xw.Workbook()  
  
sheet = wb.add_sheet("Sheet Name") 
  
# Specifying style of the elements 
style_value= xw.easyxf('font: bold 1') 
  
# Specifying column value with styling
sheet.write(0, 0, 'CodeSpeedy', style_value) 
wb.save("codespeedy.xls")
Output: An excel sheet is created with data displayed in the form of

style_value

 specification.

.easyxf() function is used to specify the styling of the data

Adding multiple style parameters at a time

# importing xlwt module 
import xlwt as xw
  
wb= xw.Workbook()  
  
sheet = wb.add_sheet("Sheet Name") 
  
# Applying multiple styles 
style = xw.easyxf('font: bold 1, color blue;') 
  
# Writing on specified sheet 
sheet.write(0, 0, 'CodeSpeedy', style) 
  
wb.save("codespeedy.xls")
Output: An excel sheet with text displayed in blue colour and in bold 1 type of styling.

Here we have specified two attributes inside the xw.easyxf() function. One is the font type and another being the font colour.

Also, learn
sep and end parameter in python 3.x.
How to create 2D array from list of lists in Python

Leave a Reply

Your email address will not be published. Required fields are marked *