My Excel workbook not closed by executing the highlighted last statement.But throws the error as below..
My code....
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible= True
excelApl = excel.Application.Workbooks.Open ("C:\Technical\AutomationScripts\TestExcelCOM.xlsx")
a = excelApl.Worksheets("Sheet1").Range("A2").Value = "Stay with me"
a = excelApl.Worksheets("Sheet1").Range("A2").Value
print(a)
a = excelApl.Worksheets("Sheet1").Range("A2").Value = "Hello"
print(a)
**excel.Application.Workbooks("C:\Technical\AutomationScripts\TestExcelCOM.xlsx").Close**
the last statement throws the below error....
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
>>> Stay with me
Hello
Traceback (most recent call last):
File "C:\Program Files\Python39\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 363, in RunScript
exec(codeObject, __main__.__dict__)
File "C:\Technical\AutomationScripts\Testscript.py", line 13, in <module>
excel.Application.Workbooks("C:\Technical\AutomationScripts\TestExcelCOM.xlsx").Close
File "C:\Users\jpillai1\AppData\Local\Temp\gen_py\3.9\00020813-0000-0000-C000-000000000046x0x1x9\Workbooks.py", line 198, in __call__
ret = self._oleobj_.InvokeTypes(0, LCID, 2, (13, 0), ((12, 1),),Index
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
>>>
Wanted to close the workbook.