Method
sheets=wb.worksheets
ws=sheets[0] #Using sheet index
ws2=wb[‘Sheet‘] #Using sheet name
ws3=wb.get_sheet_by_name(‘Sheet‘)
names=wb.sheetnames
ws4=wb[names[0]]
Sample Code
#Accessing Worksheets by Index or Name
#Import load_workbook function
from openpyxl import load_workbook
#Load the workbook
wb=load_workbook(filename='wb2.xlsx')
#Get the worksheet collection object
sheets=wb.worksheets
#Access the first worksheet using the index
ws=sheets[0]
#Output the title of the worksheet
print(ws.title)
#Access the worksheet by name
ws2=wb['Sheet']
#Output the title of worksheet ws2
print(ws2.title)
#Use get_sheet_by_name function to access a specified worksheet
ws3=wb.get_sheet_by_name('Sheet')
#Output the title of worksheet ws3
print(ws3.title)
#Get all worksheet names
names=wb.sheetnames
#Access the corresponding worksheet by the first name
ws4=wb[names[0]]
#Output the title of worksheet ws4
print(ws4.title)
wb.save('test.xlsx')
wb.close()
