Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 251
wordplayer Avatar asked Dec 02 '17 13:12

wordplayer


People also ask

Is pywin32 same as win32com?

win32com is a "Component Object Model" part of pywin32. pywin32 works with python3. pywin32 doesn't works with python3 after installing from pip.

What does win32com client do?

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.

What is win32com?

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.


3 Answers

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:

  1. 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()" 
    
  2. 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.

like image 197
Bharat Sesham Avatar answered Oct 20 '22 10:10

Bharat Sesham


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.

like image 8
Pechi Avatar answered Oct 20 '22 12:10

Pechi


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.

like image 5
Joel Villaviccencio Gastelu Avatar answered Oct 20 '22 12:10

Joel Villaviccencio Gastelu