Trying to alter Excel_sheet by python and totally confused in process recovery.
import win32com.client
class XlsClass:
def __init__(self ,filename=None ,*,Visible=False ,Alerts=False):
self.xlApp = win32com.client.Dispatch('Excel.Application')
self.xlApp.Visible = Visible
self.xlApp.DisplayAlerts = Alerts
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''
def __del__(self):
self.xlBook.Close()
self.xlApp.Quit()
Sometimes the code works well ,but sometimes python will raise an error just like 'self.xlApp.Visible cant be set?'. This always happened in a loop just like:
for fname in filelist:
xlbook = XlsClass(fname)
#do something
Then I have checked my 'windowstasksmanager' and Notice that
xlbook = Dispatch('Excel.Application')
wil create a process named 'EXCEL.EXE*32'.
When I type in 'xlbook.Quit()'
the process was still there!?
So may be the 'cant be set' error since this residual process?
After I call the func 'Dispatch' ,how can I totally close it?
del xlbook
Cant kill the process so how it works?
Bad in english ,waiting for help....thank you.
================================================
2014/3/10: I catch the error again and capture traceback...
Traceback (most recent call last):
File "C:\work_daily\work_RecPy\__RecLib\XlsClass.py", line 9, in __init__
self.xlApp.Visible = Visible
File "C:\Program Files\python33\lib\site-packages\win32com\client\dynamic.py",
line 576, in __setattr__
raise AttributeError("Property '%s.%s' can not be set." % (self._username_,attr))
AttributeError: Property 'Excel.Application.Visible' can not be set.
I tried
del self.xlApp
or
xlbook = None
before creating a new XlsClass() but seems not working...