Issue in using win32com to access Excel file
Solution 1
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?
Solution 2
The main reason for this attribute error is because your COM-server has shifted from late-binding (dynamic) to early binding (static).
- In Late Binding, whenever a method is called, the object is queried for the method and if it succeeds, then the call can be made.
- In Early Binding, the information of the object model is determined in advance from type information supplied by the object call. Early binding makes use of MakePy. Also, early binding is case sensitive.
There are two ways to fix this issue:
-
Use the dynamic module to force your code to work in a late-bound oriented way. Example use:
"win32com.client.dynamic.Dispatch()" instead of "win32com.client.Dispatch()"
-
Use camelcase sensitive keywords for the early bound oriented way. Example use:
"excel.Visible()" instead of "excel.VISIBLE()" or "excel.visible()"
Try out
"win32com.client.dynamic.Dispatch()" instead of "win32com.client.gencache.EnsureDispatch"
As win32com.client.gencache.EnsureDispatch forces the MakePy process.
Solution 3
Renaming the GenPy
folder should work.
It's present at: C:\Users\ _insert_username_ \AppData\Local\Temp\gen_py
Renaming it will create a new Gen_py
folder and will let you dispatch Excel properly.
Solution 4
A solution is to locate the gen_py folder (C:\Users\\AppData\Local\Temp\gen_py) and delete its content. It works for me when using the COM with another program.
Solution 5
While I trust the previous answers better understand EnsureDispatch, if you came here because you have this problem but cannot amend your code, or do not want to, I was able to fix this problem by renaming a folder. Running these lines raised the same error:
from win32com.client.gencache import EnsureDispatch
import sys
xl = EnsureDispatch("Excel.Application") # Error here
print(sys.modules[xl.__module__].__file__)
Now, unfortunately, if you get the error it's hard to know where that file is. For me it was this folder: C:\Users\<username>\AppData\Local\Temp\gen_py\
. Appending an underscore to that folder name (or deleting it) should cause the folder to be re-created when the code is re-run.
hat tips to
- https://www.pyxll.com/_forum/index.php?topic=436.0
- https://gist.github.com/rdapaz/63590adb94a46039ca4a10994dff9dbe
The pyxll link has another block of code to call for potentially another folder location. I didn't use it so i'm not posting the code here. The fix above worked for me, but just in case you can go further down the rabbit hole.
Related videos on Youtube
wordplayer
Updated on July 10, 2022Comments
-
wordplayer almost 2 years
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 codexl = 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.) -
Anshul Vyas about 4 yearsHey Joel, you should convert all code statements in coding format. For reference, you can take a look stackoverflow.com/help/how-to-answer
-
Hofbr over 3 yearsThis was the only thing that worked for me too. This is not a reliable way to put something in production as my end users will not be able problem solve like this on their machine... shame really.
-
Park over 2 yearsIt works for me. Thx