horace_vr horace_vr - 1 year ago 161
Python Question

Python openpyxl select sheet

I am writing some data into an Excel file, but I dont know how to adjust the code in order to be able to control which sheet I am writing into:

wb= load_workbook(filename)

Instead of
, how can I say something like
(this is how the VBA syntax would look like...)?


You should use .get_sheet_by_name

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

PS: You should check if your sheet in sheet names .get_sheet_names()

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