xlwt
xlwt is a Python library for generating spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files. It operates on any platform and supports Python 2.6, 2.7, and 3.3+. The current version is 1.3.0, released in 2017, and the project is effectively feature-frozen and no longer actively maintained.
Warnings
- breaking xlwt only supports the legacy .xls file format (Excel 97-2003). It cannot read or write the modern .xlsx format (Excel 2007+).
- deprecated The xlwt library is no longer actively maintained and is considered feature-frozen. It has been largely superseded by more modern libraries for Excel file manipulation.
- gotcha Due to the underlying .xls format, xlwt is limited to 65,536 rows and 256 columns per sheet. Exceeding these limits will result in errors.
- gotcha xlwt does not support many advanced Excel features such as charts, pivot tables, macros, or more complex conditional formatting, which are common in modern spreadsheets.
Install
-
pip install xlwt
Imports
- Workbook
import xlwt workbook = xlwt.Workbook()
- easyxf
import xlwt style = xlwt.easyxf('font: bold on')
Quickstart
import xlwt
# Create a new workbook
workbook = xlwt.Workbook()
# Add a sheet
sheet = workbook.add_sheet('Sheet1')
# Write some data to cells
sheet.write(0, 0, 'Name')
sheet.write(0, 1, 'Age')
sheet.write(1, 0, 'Alice')
sheet.write(1, 1, 30)
# Add some styling
style = xlwt.easyxf('font: bold on; align: horiz center')
sheet.write(2, 0, 'Bob', style)
sheet.write(2, 1, 24, style)
# Save the workbook
file_path = 'example.xls'
try:
workbook.save(file_path)
print(f'Successfully created {file_path}')
except Exception as e:
print(f'Error saving workbook: {e}')