Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeze Panes first two rows and column with openpyxl

Trying to freeze the first two rows and first column with openpyxl, however, whenever doing such Excel says that the file is corrupted and there is no freeze.

Current code:

workbook = openpyxl.load_workbook(path)
worksheet = workbook[first_sheet]
freeze_panes = Pane(xSplit=2000, ySplit=3000, state="frozen", activePane="bottomRight")
worksheet.sheet_view.pane = freeze_panes

Took a look at the documentation, however, there is little explanation on parametere setting.

Desired output: enter image description here

Came across this answer, however, it fits a specific use case, hence, wanted to make a general question for future reference: How to split Excel screen with Openpyxl?

like image 282
Akmal Soliev Avatar asked Sep 12 '25 14:09

Akmal Soliev


1 Answers

To freeze the first two rows and first column, use the sample code below... ws.freeze_panes works. Note that, like you would do in excel, select the cell above and left of which you want to freeze. So, in your case, the cell should be B3. Hope this is what you are looking for.

import openpyxl
wb=openpyxl.load_workbook('Sample.xlsx')
ws=wb['Sheet1']
mycell = ws['B3']
ws.freeze_panes = mycell 
wb.save('Sample.xlsx')
like image 149
Redox Avatar answered Sep 14 '25 04:09

Redox