Skip to content
Advertisement

How to automatically display a specific range of columns on a spreadsheet created with xlsxwriter?

I would like to automatically display a specific range of columns when opening an excel spreadsheet created with xlsxwriter.

This is illustrated in the example below where the first row and column are frozen while the range of columns being displayed start at E and not at B (but note that I don’t want B:D to be hidden, I just want to start the range of column displayed at E)

Is this doable?

enter image description here

Advertisement

Answer

The XlsxWriter worksheet.freeze_panes() method has 2 optional parameters (top_row, left_column) which can be used to set the first cell in the non-frozen area. Like this:

import xlsxwriter

workbook = xlsxwriter.Workbook('panes.xlsx')
worksheet = workbook.add_worksheet()
side_format = workbook.add_format({'bold': True,
                                   'fg_color': '#D7E4BC',
                                   'border': 1})

worksheet.freeze_panes(0, 1, 0, 4)

# Some sample data.
for row_num in range(0, 50):
    worksheet.write(row_num, 0, 'Frozen', side_format)
    for col_num in range(1, 26):
        worksheet.write(row_num, col_num, row_num + 1)

workbook.close()

Output:

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement