XLWT multiple styles

Jon Hagelin picture Jon Hagelin · Aug 16, 2013 · Viewed 22.4k times · Source

This has been bogging my mind with my current project. I'm trying to write styles into an excel sheet using XLWT, see below:

sheet.write(rowi,coli,value,stylesheet.bold,stylesheet.bordered)

I'm running into this error:

TypeError: write() takes at most 5 arguments (6 given)

Any idea how to get around this to add multiple styles to a certain cell? Is it possible to do a list here?

Answer

alecxe picture alecxe · Aug 16, 2013

You should pass only row number, col number, value and style (XFStyle class instance) to the write method, for example:

import xlwt

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Test')

style = xlwt.XFStyle()

# font
font = xlwt.Font()
font.bold = True
style.font = font

# borders
borders = xlwt.Borders()
borders.bottom = xlwt.Borders.DASHED
style.borders = borders

worksheet.write(0, 0, 'test value', style=style)
workbook.save('test.xls')

The same thing, but using easyxf:

import xlwt

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Test')  

style_string = "font: bold on; borders: bottom dashed"
style = xlwt.easyxf(style_string)

worksheet.write(0, 0, 'test value', style=style)
workbook.save('test.xls')