How to freeze entire header row in openpyxl?
Question:
How to freeze entire header row in openpyxl?
So far I can only freeze the column:
# only freeze the column (freeze vertically)
cell = ws.cell('{}{}'.format(col, row_idx+1))
worksheet.freeze_panes = cell
Answers:
Make sure cell
isn’t on row one – freeze_panes
will freeze rows above the given cell and columns to the left.
Example:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
c = ws['B2']
ws.freeze_panes = c
wb.save('test.xlsx')
This will give you a blank worksheet with both row 1 and column A frozen.
For spreadsheets too large to be displayed all at once, it’s helpful to “freeze” a few of the top rows or leftmost columns onscreen. Frozen column or row headers, for example, are always visible to the user even as they scroll through the spreadsheet. These are known as freeze panes. In OpenPyXL, each Worksheet object has a freeze_panes attribute that can be set to a Cell object or a string of a cell’s coordinates. Note that all rows above and all columns to the left of this cell will be frozen, but the row and column of the cell itself will not be frozen.
To unfreeze all panes, set freeze_panes to None or ‘A1’. Table 13-3 shows which rows and columns will be frozen for some example settings of freeze_panes.
Table 13-3: Frozen Pane Examples
freeze_panes setting
Rows and columns frozen
sheet.freeze_panes = ‘A2’
Row 1
sheet.freeze_panes = ‘B1’
Column A
sheet.freeze_panes = ‘C1’
Columns A and B
sheet.freeze_panes = ‘C2’
Row 1 and columns A and B
How to freeze entire header row in openpyxl?
So far I can only freeze the column:
# only freeze the column (freeze vertically)
cell = ws.cell('{}{}'.format(col, row_idx+1))
worksheet.freeze_panes = cell
Make sure cell
isn’t on row one – freeze_panes
will freeze rows above the given cell and columns to the left.
Example:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
c = ws['B2']
ws.freeze_panes = c
wb.save('test.xlsx')
This will give you a blank worksheet with both row 1 and column A frozen.
For spreadsheets too large to be displayed all at once, it’s helpful to “freeze” a few of the top rows or leftmost columns onscreen. Frozen column or row headers, for example, are always visible to the user even as they scroll through the spreadsheet. These are known as freeze panes. In OpenPyXL, each Worksheet object has a freeze_panes attribute that can be set to a Cell object or a string of a cell’s coordinates. Note that all rows above and all columns to the left of this cell will be frozen, but the row and column of the cell itself will not be frozen.
To unfreeze all panes, set freeze_panes to None or ‘A1’. Table 13-3 shows which rows and columns will be frozen for some example settings of freeze_panes.
Table 13-3: Frozen Pane Examples
freeze_panes setting
Rows and columns frozen
sheet.freeze_panes = ‘A2’
Row 1
sheet.freeze_panes = ‘B1’
Column A
sheet.freeze_panes = ‘C1’
Columns A and B
sheet.freeze_panes = ‘C2’
Row 1 and columns A and B