2

My python code raises an error when creating the necessary gen_py folder in the \Appdata\Local\Temp\ 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)\Python\lib\site-packages\win32com\client\gencache.py", line 531, in EnsureDispatch
    mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 515, in EnsureModule
    module = MakeModuleForTypelib(typelibCLSID, lcid, major, minor, progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 284, in MakeModuleForTypelib
    makepy.GenerateFromTypeLibSpec( (typelibCLSID, lcid, major, minor), progressInstance=progressInstance, bForDemand = bForDemand, bBuildHidden = bBuildHidden)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\makepy.py", line 279, in GenerateFromTypeLibSpec
    gen.generate(fileUse, bForDemand)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 822, in generate
    self.do_generate()
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 885, in do_generate
    oleItems, enumItems, recordItems, vtableItems = self.BuildOleItemsFromType()
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 750, in BuildOleItemsFromType
    oleItem, vtableItem = self._Build_Interface(type_info_tuple)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 718, in _Build_Interface
    oleItem = DispatchItem(info, attr, doc)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\genpy.py", line 270, in __init__
    build.DispatchItem.__init__(self, typeinfo, attr, doc)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\build.py", line 115, in __init__
    self.Build(typeinfo, attr, bForUser)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\build.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:\Python\file.py", line 659, in <module>
    start()
  File "D:\Python\file.py", line 55, in start
    AE_addPivotTables(str_filename, df_opzet)
  File "D:\Python\file.py", line 556, in AE_addPivotTables
    Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.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 \Appdata\Local\Temp\ 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:\Python\file.py", line 659, in <module>
    start()
  File "D:\Python\file.py", line 55, in start
    AE_addPivotTables(str_filename, df_opzet)
  File "D:\Python\file.py", line 556, in AE_addPivotTables
    Excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # Excel = win32com.client.Dispatch('Excel.Application')
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 531, in EnsureDispatch
    mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 388, in EnsureModule
    module = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.py", line 263, in GetModuleForTypelib
    AddModuleToCache(typelibCLSID, lcid, major, minor)
  File "C:\Program Files (x86)\Python\lib\site-packages\win32com\client\gencache.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)\Python\Lib\site-packages\pywin32_system32 to C:\Program Files (x86)\Python\Lib\site-packages\win32\lib. 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.

  • Does this answer your question? [python-win32com excel com model started generating errors](https://stackoverflow.com/questions/52889704/python-win32com-excel-com-model-started-generating-errors) – Julienm Apr 03 '21 at 13:16

0 Answers0