How can I force python(using win32com) to create a new instance of excel?

后端 未结 2 1610
失恋的感觉
失恋的感觉 2020-12-15 08:44

I\'m automating some excel related tasks which take a long time.

I\'m creating an excel instance using:

excel = win32.gencache.EnsureDispatch(\'Excel         


        
2条回答
  •  遥遥无期
    2020-12-15 09:11

    Here's a way to create a new instance and use static cache (which is faster and gives an ability to use kwargs):

    from win32com.client import gencache
    import pythoncom
    
    clsid = "Word.Application"
    clsid = pythoncom.CoCreateInstanceEx(clsid, None, pythoncom.CLSCTX_SERVER,
                                         None, (pythoncom.IID_IDispatch,))[0]
    if gencache.is_readonly:
        #fix for "freezed" app: py2exe.org/index.cgi/UsingEnsureDispatch
        gencache.is_readonly = False
        gencache.Rebuild()
    olApp = gencache.EnsureDispatch(clsid)
    

提交回复
热议问题