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

后端 未结 2 1609
失恋的感觉
失恋的感觉 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:05

    Why don't you do it like this?

    from win32com import client
    excel=client.Dispatch("Excel.Application")
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题