openpyxl get sheet by name

import openpyxl

n = 0
wb = openpyxl.load_workbook('D:\excel.xlsx')
sheets = wb.sheetnames
ws = wb[sheets[n]]

the refernce: How to switch between sheets in excel openpyxl python to make changes


You should use wb[sheetname]

from openpyxl import load_workbook
wb2 = load_workbook('test.xlsx')
ws4 = wb2["New Title"]

PS: You should check if your sheet in sheet names wb.sheetnames

print(wb2.sheetnames)
['Sheet2', 'New Title', 'Sheet1']

Fix your openpyxl version. Upgrading from 2.3.2 to 2.4.10 will fix this issue. Do this:

pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org openpyxl==2.4.10