How to load a workbook and at the same time go to a specific sheet? using Openpyxl

Viewed 87

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 Answers

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']
Related