8

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Jon Hagelin
  • 137
  • 1
  • 1
  • 6

1 Answers1

20

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')
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    Ah, yeah. Makes sense. I was doing it this way previously, but it just seems bulky. I found out about xlwt.easyxf and thought there would be an easy way to do it with that. Guess not though ): – Jon Hagelin Aug 16 '13 at 18:41
  • You can use `easyxf` too. It's basically a way of initializing your xlwt styles in a string. I can provide you an example, if you want. – alecxe Aug 16 '13 at 18:42
  • Yes please. I don't mean to be a burden, but I really just need to see how it's done. – Jon Hagelin Aug 16 '13 at 18:45
  • One last question for you, my apologies. If I had two styles in a stylesheet, is there anyway to join the two styles into one string so I could in theory swap them out? There are different styles for different functions in my spreadsheet. – Jon Hagelin Aug 16 '13 at 18:57