5

I need to stylize certain cells and rows in an xls file I create through my program, but I am having a few issues, possible misconceptions about how the xlwt easyxf stuff works.

First, if I write to the cell without a value and just the style, does the value inside get erased?

Second, I'm trying to write to cells using the style and value of the cell, but I keep getting an error:

"TypeError: 'XFStyle' object is not callable". -Solved

Now the problem is that the styles do not get implemented. After writing the cells, and outputting it to a xls file, there were no color,bg,size,font changes at all.

I tried googling this and was following other people's examples, but for whatever reason, my code doesn't work. Here it is:

def stylize_spreadsheet_accordingly(iFile, workbook, row_grey, row_underline, row_font_size, cells_yellow):
    #this time iFile is the file you're overwriting

    #styling stuff

    print "styling the document..."

    new_sheet.col(0).width = 256 * 18
    new_sheet.col(1).width = 256 * 69.43
    new_sheet.col(2).width = 256 * 9
    new_sheet.col(3).width = 256 * 20.71
    new_sheet.col(4).width = 256 * 8.43

    font_size_style = xlwt.easyxf('font: name Calibri, bold on, height 280;')
    font_underline_style = xlwt.easyxf('font: underline on;')
    fill_grey_style = xlwt.easyxf('pattern: back_color gray25;')
    fill_yellow_style = xlwt.easyxf('pattern: back_color yellow;')

    iBook = open_workbook(iFile)
    iSheet = iBook.sheet_by_index(0)

    for row_index in range(iSheet.nrows):
        if row_index in row_grey:
            for col_index in range(iSheet.ncols):
                new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style)
        if row_index in row_underline:
            for col_index in range(iSheet.ncols):
                new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, font_underline_style)
        if row_index in row_font_size:
            for col_index in range(iSheet.ncols):
                new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, font_size_style)
    for each in cells_yellow:
        new_sheet.write(each[0], each[1], iSheet.cell(each[0],each[1]).value, fill_yellow_style)

    return workbook

new_sheet is a global variable that I made in another function that represents the sheet I added to my xlwt workbook. The workbook I pass in, is the file that is supposed to contain that new_sheet. I might be over complicating it or doing it unethically, but it works.

P.S. If there's a different way I could do this or change certain cells to be a full color in a different manner, please let me know. Once again, thanks.

Thanks, I fixed the code to what you guys said, and the TypeError went away, but after it completes, none of the styling options I created and used, went through. The xls file was still in its default format. How can this be?

Michael M
  • 466
  • 2
  • 7
  • 14

3 Answers3

2

You are getting 'XFStyle' object is not callable because you are calling it like a function instead of just passing it to sheet.write e.g.

instead of

new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style())

use

new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style)
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • Thanks, but for whatever reason, none of my styles are being enacted. Everything works, gets compiled and runs, but when I open the xls document, there is no change in font or color. The only thing that does change are the column widths. – Michael M Aug 17 '12 at 03:55
0

You're creating the styles and then attempting to call them.

For example:

font_size_style = xlwt.easyxf('font: name Calibri, bold on, height 280;')
...
new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style())

Notice this bit:

fill_grey_style()
MRAB
  • 20,356
  • 6
  • 40
  • 33
0

The following snippet may help you, I use this code to fill the columns with grey color

dep_style = xlwt.easyxf('font: bold on,height 200;''align: horiz left;''borders: left 
thin, right thin, top thin, bottom thin')

import xlwt
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = xlwt.Style.colour_map['gray25']
dep_style.pattern = pattern
sheet1.write_merge(s2,s2,0,int(count[0])+13,dep_data['department_name'],dep_style)

The above codes gives me row in grey color with Name attached to it. You can alter this code for your requirement

Nithin
  • 137
  • 1
  • 1
  • 6