horace_vr horace_vr - 2 years ago 375
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...)?

Answer Source

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']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download