Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeze cells in excel using xlwt

Tags:

python

excel

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')
like image 815
Raghav Shaligram Avatar asked Jul 26 '14 18:07

Raghav Shaligram


People also ask

How do I freeze a column in Xlsx?

Freeze columns and rows Select the cell below the rows and to the right of the columns you want to keep visible when you scroll. Select View > Freeze Panes > Freeze Panes.

How do I merge cells in Excel with XLWT?

There are two methods on the Worksheet class to do this, write_merge and merge . merge takes existing cells and merges them, while write_merge writes a label (just like write ) and then does the same stuff merge does. Both take the cells to merge as r1, r2, c1, c2 , and accept an optional style parameter.


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 frozen take effect.

like image 135
Stephen Lin Avatar answered Sep 28 '22 15:09

Stephen Lin


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)
like image 26
Cashiuus Avatar answered Sep 28 '22 17:09

Cashiuus