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
415 views
in Technique[技术] by (71.8m points)

python - win32com EnsureDispatch fails to load DLL at creating gen_py cache

My python code raises an error when creating the necessary gen_py folder in the AppdataLocalTemp directory. The error is raised when executing the following line:

Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')

When executing this line (for the first time) the following error is raised:

Traceback (most recent call last):
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 531, in EnsureDispatch
    mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 515, in EnsureModule
    module = MakeModuleForTypelib(typelibCLSID, lcid, major, minor, progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 284, in MakeModuleForTypelib
    makepy.GenerateFromTypeLibSpec( (typelibCLSID, lcid, major, minor), progressInstance=progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientmakepy.py", line 279, in GenerateFromTypeLibSpec
    gen.generate(fileUse, bForDemand)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgenpy.py", line 822, in generate
    self.do_generate()
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgenpy.py", line 885, in do_generate
    oleItems, enumItems, recordItems, vtableItems = self.BuildOleItemsFromType()
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgenpy.py", line 750, in BuildOleItemsFromType
    oleItem, vtableItem = self._Build_Interface(type_info_tuple)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgenpy.py", line 718, in _Build_Interface
    oleItem = DispatchItem(info, attr, doc)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgenpy.py", line 270, in __init__
    build.DispatchItem.__init__(self, typeinfo, attr, doc)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientuild.py", line 115, in __init__
    self.Build(typeinfo, attr, bForUser)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientuild.py", line 262, in Build
    fdesc = typeinfo.GetFuncDesc(j)
pywintypes.com_error: (-2147312566, 'Fout bij laden van type DLL-bestand.', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:Pythonfile.py", line 659, in <module>
    start()
  File "D:Pythonfile.py", line 55, in start
    AE_addPivotTables(str_filename, df_opzet)
  File "D:Pythonfile.py", line 556, in AE_addPivotTables
    Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 538, in EnsureDispatch
    raise TypeError("This COM object can not automate the makepy process - please run makepy manually for this object")
TypeError: This COM object can not automate the makepy process - please run makepy manually for this object

Manually running the makepy process raises the same error.

After this error I see in the directory AppdataLocalTemp that the folder 'gen_py' has been created with the expected files in the cache. However my code is not dispatching excel yet because when I run the script a second time the following error is raised:

Traceback (most recent call last):
  File "D:Pythonfile.py", line 659, in <module>
    start()
  File "D:Pythonfile.py", line 55, in start
    AE_addPivotTables(str_filename, df_opzet)
  File "D:Pythonfile.py", line 556, in AE_addPivotTables
    Excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # Excel = win32com.client.Dispatch('Excel.Application')
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 531, in EnsureDispatch
    mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 388, in EnsureModule
    module = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 263, in GetModuleForTypelib
    AddModuleToCache(typelibCLSID, lcid, major, minor)
  File "C:Program Files (x86)Pythonlibsite-packageswin32comclientgencache.py", line 549, in AddModuleToCache
    dict = mod.CLSIDToClassMap
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToClassMap'

It seems that the first error raised when creating the gen_py folder results in a cache that cannot be used. The problem seems to be in loading DLL files by pywintypes.com.

I tried this solution of a similar problem: https://github.com/ContinuumIO/anaconda-issues/issues/37 I copied the .dll files from the map C:Program Files (x86)PythonLibsite-packagespywin32_system32 to C:Program Files (x86)PythonLibsite-packageswin32lib. However this did not solve the issue.

Is there a way to solve this error?

Thanks in advance!

EDIT: Removing and re-installing Python and all necessary packages did not resolve the issue.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...