Openpyxl || Freezing excel row and column with python.

Freezing a pane(row/column) using excel application is easy. We can freeze a row/column using excel application from the “Freeze Panes” option available in the “View” tab of the menu ribbon as shown in the screenshot below.

We can automate this thing using an openpyxl module in python as well. let’s suppose we want to freeze the first row of the input sheet.

Inuput

To freeze the first row of the input sheet we will assign the A2  cell to the freeze_pane object.
sheet.freeze_panes = ‘A2’

Source code

import openpyxl
from openpyxl.styles import Alignment
workbook=openpyxl.load_workbook('openpy102.xlsx')
sheet = workbook['daily sales']
sheet.freeze_panes = 'A2'
workbook.save('openpy102.xlsx')

Save the above code as freeze_openpyxl.py and execute it as shown in the below snapshot.

Output:

As we freeze the first row, we can freeze multiple rows as well. Suppose if we want to freeze the first 3 rows, we will assign A4 cell to freeze_panes object as follows.

If we assign the B2 instead of A2 in the above source code (sheet.freeze_panes = ‘B2’) it will freeze the first row and column as shown in below snapshot.

One comment

Comments are closed.