14

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

I'm creating an excel instance using:

excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()

however, after the script starts running, if i select an open excel workbook(not the one python is working on), The python script crashes. However, if I open a new excel workbook and type stuff into it, the python script is unaffected.

Is there a particular way I can call excel to prevent this from happening? Or any other solution?

EDIT: This seems to work.

excel = win32.DispatchEx('Excel.Application')
4
  • It wouldn't hurt if you would also mention what versions are you using just in case. Commented Mar 15, 2011 at 10:30
  • well whats the situation with the question? Did you find an answer? Please let us know. Commented Jan 11, 2012 at 16:18
  • Yes, I did find a solution, see the edit. Commented Jan 12, 2012 at 6:24
  • @jck You should post this as answer instead of editing it in the question Commented Feb 15, 2017 at 12:46

2 Answers 2

9

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

import sys
import shutil
import pythoncom
from win32com.client import gencache

def EnsureDispatchEx(clsid, new_instance=True):
    """Create a new COM instance and ensure cache is built,
       unset read-only gencache flag"""
    if new_instance:
        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()
    try:
        return gencache.EnsureDispatch(clsid)
    except (KeyError, AttributeError):  # no attribute 'CLSIDToClassMap'
        # something went wrong, reset cache
        shutil.rmtree(gencache.GetGeneratePath())
        for i in [i for i in sys.modules if i.startswith("win32com.gen_py.")]:
            del sys.modules[i]
        return gencache.EnsureDispatch(clsid)

wdApp = EnsureDispatchEx("Word.Application")

Upd: improved version resets cache on error

Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you do it like this?

from win32com import client
excel=client.Dispatch("Excel.Application")

2 Comments

If you don't run the make.py to generate a "static com proxy" then EnsureDispatch is required to access the win32com.constants. See this link: timgolden.me.uk/python/win32_how_do_i/… Furthermore, Dispatch doesn't solve his problem, the solution is DispatchEx()
This won't start a new Excel process if one is already running as the same user. See stackoverflow.com/questions/52571993/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.