How to freeze first column using Openpyxl

Viewed 30

I am using the following code to freeze the first column of my excel sheer generated using openpyxl. But this is not working and showing me an error.

dim_holder = DimensionHolder(worksheet=worksheet)
    for col in range(worksheet.min_column, worksheet.max_column + 1):
        dim_holder[get_column_letter(col)] = ColumnDimension(
            worksheet, min=col, max=col, width=20)

    # worksheet.column_dimensions.group(2, 10)
    dim_holder['A'] = ColumnDimension(
        worksheet, min=1, max=1, width=35)

    worksheet.column_dimensions = dim_holder
    worksheet.freeze_panes = 'A'

How can I freeze the entire first column, so that it will be sticky when I do horizontal scrolling ?

1 Answers

As not all code is present, but you have mentioned that you are using openpyxl, I am going to assume that. To freeze panes, you need to give the cell. That will freeze the panes above and to the left of the same. So, when you give A, it will not help, need to change it to B1 instead. A simple example is given below.

wb=openpyxl.load_workbook('YourFile.xlsx')
ws=wb['YourSheet']
c = ws['B1']  ## Freeze everything to left of B (that is A) and no columns to feeze
ws.freeze_panes = c
wb.save('YourFile.xlsx')
Related