Freeze cells in excel using xlwt

I create worksheets on the fly and without calling them anything. I can not freeze the first column and row. I am tired of working with the sheet when adding it to the book, and it works. However, it does not 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')
+4
source share
2 answers

You have to use

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

to make the effect of freezing.

+9
source

The reason this does not work may be the result of the get_sheet () function. Instead, save the add_sheet () call to "ws" and use this:

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

And you will need this sequence of attributes to freeze the top line:

#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

, , .

, :

ws.set_panes_frozen(True)
ws.set_remove_splits(True)
+2

All Articles