Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python xlwt : using easyxf to stylize cells when writing bug

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?

like image 569
Michael M Avatar asked Nov 03 '22 18:11

Michael M


1 Answers

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)
like image 141
Anurag Uniyal Avatar answered Nov 12 '22 17:11

Anurag Uniyal