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.)
win32com is a "Component Object Model" part of pywin32. pywin32 works with python3. pywin32 doesn't works with python3 after installing from pip.
The win32com. client package contains a number of modules to provide access to automation objects. This package supports both late and early bindings, as we will discuss.
win32com.server package Support for COM servers written in Python. The modules in this package provide most of the underlying framework for magically turning Python classes into COM servers, exposing the correct public methods, registering your server in the registry, etc.
The main reason for this attribute error is because your COM-server has shifted from late-binding (dynamic) to early binding (static).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With