Search code examples
pythonexcelwin32com

Issue in using win32com to access Excel file


everyone!
I have been using the win32com.client module in Python to access cells of an Excel file containing VBA Macros.
A statement in the code xl = win32com.client.gencache.EnsureDispatch("Excel.Application") has been throwing an error:
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x6' has no attribute 'MinorVersion'
Has anyone faced a similar situation and, if yes, what can a possible remedy for this? (I've had a look at the source code for win32com on GitHub, but haven't been able to make much sense from it.)


Solution

  • If I want to make sure that python starts a new excel instance (for instance to access macros in my xlsm files), I use

    xlApp = win32com.client.DispatchEx("Excel.Application")
    

    That way I can close the application without hurting the instance that was already open.

    Otherwise, I could simply use

    xlApp = win32com.client.Dispatch("Excel.Application")
    

    Does that work for you?