Raghav Shaligram
Raghav Shaligram

Reputation: 309

Freeze cells in excel using xlwt

I am creating worksheets on a fly and not naming them anything. I am unable to freeze the first column and row. I tired working with naming the sheet when adding it to the workbook and it works. However doesn't work on the fly. Below is the code

base = xlwt.Workbook()
for k,v in MainDict.items():
    base.add_sheet(k.upper())
    col_width = 256 * 50
    xlwt.add_palette_colour("custom_colour", 0x21)
    pattern = 'url:(.*)'
    search = re.compile(pattern)
    base.set_colour_RGB(0x21, 251, 228, 228)
    style = xlwt.easyxf('pattern: pattern solid, fore_colour custom_colour;font : bold on;alignment: horiz center;font: name Times New Roman size 20;font:underline single')
    index = MainDict.keys().index(k)
    ws = base.get_sheet(index)
    ws.set_panes_frozen(True)
    try:
        for i in itertools.count():
            ws.col(i).width = col_width
    except ValueError:
        pass
    style1 = xlwt.easyxf('font: name Times New Roman size 15')
    style2 = xlwt.easyxf('font : bold on;font: name Times New Roman size 12')
    col=0
    for sk in MainDict[k].keys():
        ws.write(0,col,sk.upper(),style)
        col+=1
        row =1
        for mk in MainDict[k][sk].keys():
            for lk,lv in MainDict[k][sk][mk].items():
                for items in lv:
                    text = ('%s URL: %s')%(items,lk)
                    links =('No data Found. Please visit the URL: %s')% (lk)
                    url = re.findall(pattern,text)
                    if len(items) != 0:
                        if re.match(pattern,text)==True:
                            ws.write(row,col-1,url,style2)
                        else:                       
                            ws.write(row,col-1,text,style1)
                            row+=1
                    else:
                        ws.write(row,col-1,links,style2)
                     #ws.Column(col-1,ws).width = 10000
                        row+=1
default_book_style = base.default_style
default_book_style.font.height = 20 * 36
base.save('project7.xls')

Upvotes: 9

Views: 4236

Answers (2)

Cashiuus
Cashiuus

Reputation: 136

The reason this isn't working may be the result of the "get_sheet() function. Instead, store the add_sheet() call to "ws" and use that:

#base.add_sheet(k.upper())
ws = base.add_sheet(k.upper())

And then you need this sequence of attributes to freeze top row:

#ws = base.get_sheet(index)
#ws.set_panes_frozen(True)
ws.set_horz_split_pos(1)
ws.set_vert_split_pos(1)
ws.panes_frozen = True
ws.remove_splits = True

I tested this using your code snippet and it works on my end.

For reference, you can set these attributes either via function or as assignment:

ws.set_panes_frozen(True)
ws.set_remove_splits(True)

Upvotes: 3

Stephen Lin
Stephen Lin

Reputation: 4912

You have to use

    ws.set_panes_frozen(True)
    ws.set_horz_split_pos(1) 
    ws.set_vert_split_pos(1) 

to make frozen take effect.

Upvotes: 16

Related Questions