0

The below code excel application is opening but not closing for some reason.Help me on this.

import win32com.client as win32
Filepath = r"C:\New_Technicals\Excel_devp_Test\Model_Variable_Pah_v1.xlsx"
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible= True
excelApl= excel.Application.Workbooks.Open(Filepath)

ECU_OutSgls_ls =['xxx','yyy','zzz']
Path_ECU_OutSgls =[]

i=0
while i < len(ECU_OutSgls_ls):
c = excelApl.Worksheets("aaa").Range("A1:A100").Find(ECU_OutSgls_ls[i])
Path_ECU_OutSgls.append( excelApl.Worksheets("aaa").Range(c.Address).Offset(1,2))
i= i+1


excel.Application.ActiveWorkbook.Close     
2
  • Have you tried excel.Application.ActiveWorkbook.Close(True)? Commented Mar 17, 2023 at 23:07
  • No luck. Its still not closing.. Commented Mar 18, 2023 at 15:39

1 Answer 1

0

In the following line of code:

excelApl= excel.Application.Workbooks.Open(Filepath)

The Application property is not required. You already have an Application instance returned from the EnsureDispatch function.

The Open method opens a workbook and returns a Workbook object that represents the opened workbook. So, you could should look like that instead:

workbook = excel.Workbooks.Open(Filepath)

And when you need to close the workbook you may simply call the Close method:

workbook.Close  

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.