Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XLWT multiple styles

Tags:

python

excel

xlwt

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?

like image 910
Jon Hagelin Avatar asked Aug 16 '13 18:08

Jon Hagelin


1 Answers

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')
like image 143
alecxe Avatar answered Sep 22 '22 06:09

alecxe