Python xlwt set Excel Column Width

In this Python xlwt tutorial we learn how to adjust the column width of exported Excel file.

Install xlwt package

Firstly we need to install the xlwt package by the command below.

pip install xlwt

How to set column width of Excel file using Python xlwt

To specify the column width during writing the Excel file we can access the column by its index and set the width value.

sheet.col(0).width = 7000
sheet.col(1).width = 20000

The following example Python program will show you how to write Excel file with given column width of first two column.

column-width.py

import xlwt

workbook = xlwt.Workbook()
sheet = workbook.add_sheet("Sample")

header_font = xlwt.Font()
header_font.name = 'Arial'
header_font.bold = True

header_style = xlwt.XFStyle()
header_style.font = header_font

sheet.write(0, 0, 'Title', header_style)
sheet.write(0, 1, 'Description', header_style)

sheet.write(1, 0, 'Tori Code')
sheet.write(1, 1, 'Python Excel Tutorials')

sheet.col(0).width = 7000
sheet.col(1).width = 20000

workbook.save('sample.xls')

Run the program.

python column-width.py

The output Excel file.

Python xlwt set Excel Column Width

Happy Coding 😊