Home > Back-end >  Freeze first row of all worksheets in workbook (Python)
Freeze first row of all worksheets in workbook (Python)

Time:08-16

Quick question.

I am using xlsxwriter to edit my worksheets. I have 7 worksheets in my workbook and I'd like to freeze the first row of these 7 worksheet. For this purpose:

worksheet.freeze_panes(1, 0) 

worked out for me. However, I wrote this line for each worksheet.

Then, I tried to iterate over a list of worksheet names as follows:

sheets = ['worksheet', 'worksheet2', 'worksheet3', 'worksheet4', 'worksheet5', 'worksheet6', 'worksheet7', 'worksheet8']
        
        for i in range(len(sheets)):
            sheets[i].freeze_panes(1, 0)

I got the error message: AttributeError: 'str' object has no attribute 'freeze_panes'

Is there a way to do this? Not calling sheet by sheet?

Thank you in advance!

** UPDATE **

for i in range(len(sheets)):
   workbook.get_worksheet_by_name(sheets[i]).freeze_panes(1, 0) 

or

for sheet in sheets: 
   workbook.get_worksheet_by_name(sheets[sheet]).freeze_panes(1, 0) 

Not working either.

CodePudding user response:

Assuming you want to freeze panes on every worksheet just do

worksheet in workbook.worksheets():
    worksheet.freeze_panes(1, 0)
  • Related