Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python winapi check if sheet exists

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 ?

like image 484
der_die_das_jojo Avatar asked Jun 28 '26 19:06

der_die_das_jojo


1 Answers

'name' in [excel_file.Sheets(i).Name for i in range(1,excel_file.Sheets.Count+1)]
like image 57
vikash Avatar answered Jul 01 '26 08:07

vikash