1

I already have a code to load a workbook and add a new sheet as well. How can I transfer to that new sheet added after loading the workbook? I am using openpyxl

workbook = openpyxl.load_workbook('Sample File.xlsx')
workbook.create_sheet('Sample Sheet')
workbook.save(filename='Sample File.xlsx')

Any tips?

1 Answers1

0

Just catch the sheet thrown by create_sheet:

ws = workbook.create_sheet('Sample Sheet')

"ws" above contains the created sheet. Alternatively, you can load any sheet in a workbook using:

ws = workbook['Sample Sheet']
Ibrahim
  • 71
  • 4
  • get_sheet_by_name is already not available with the openpyxl version I am using which is 3.0.9, what can be an alternative for this? – Daniel Ford May 17 '22 at 06:27
  • Yes it's been a while since I used openpyxl. It might have been deprecated. Check this question: https://stackoverflow.com/questions/36814050/openpyxl-get-sheet-by-name – Ibrahim May 17 '22 at 06:32