Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...