Reputation: 913
i am using this:
from win32com.client import Dispatch
excel_file = Dispatch("Excel.Application")
excel_file.Workbooks.Open(excel_result_path)
excel_file.Visible = 1
mySheet = excel_file.Worksheets.Add()
mySheet.Name = "name"
which works fine. the only problem is, if the sheet allready exists, i get an error telling me, that the sheet already exists
File "..\dynamic.py", line 554, in setattr pywintypes.com_error: (-2147352567, 'Ausnahmefehler aufgetreten.', (0, 'Microsof t Excel', 'Kann einem Blatt nicht den gleichen Namen geben wie einem anderen Bla tt, einer Objektbibliothek oder einer Arbeitsmappe, auf die Visual Basic Bezug n immt.', 'xlmain11.chm', 0, -2146827284), None)
so my question is, how can i check if an excel-sheet-name allready exists ?
Upvotes: 3
Views: 2949
Reputation: 322
Slightly more readable solution:
'name' in [sheet.Name for sheet in excel_file.Sheets]
Upvotes: 0
Reputation: 66
'name' in [excel_file.Sheets(i).Name for i in range(1,excel_file.Sheets.Count+1)]
Upvotes: 5