How to work around `pywintypes.com_error` using pyrtd/pythoncom?

11,862

Solved. Install and use 32-bit ActivePython

http://python.6.x6.nabble.com/Problem-using-win32com-client-Dispatch-on-Win7-64bit-td1957248.html

It may because I am using 64-bit python and trying to get 32-bit com object

Share:
11,862
Eric Wang
Author by

Eric Wang

Quant Analyst, Python User

Updated on June 14, 2022

Comments

  • Eric Wang
    Eric Wang almost 2 years

    I am trying to use the pyrtd module for Python to get the information which can be got in Excel through RTD.

    However, when I try to run the sample python script, I get the following error:

    pywintypes.com_error:( -2147221164, 'Class not registered', None, None)
    

    I have tried several other RTD function and sometimes (When I am using Dllname.function_name as the parameter for RTDClient()) I get:

    pywintypes.com_error:( -2147221005, 'Invalid class string', None, None)
    

    All of these RTD functions work well in Excel 2010.

    I have not found a solution for Python yet, but I have gone through some of the tips available for C#:

    The basic idea is:

    1. Get the class type of rtd server from register table by ProgID (for example, RTDTime.RTD, you can find them in HKEY_LOCAL_MACHINE\SOFTWARE\Classes\RTDTime.RTD, if you have already used regsvr32 to register the dll).

      In C#, the function is Type.GetTypeFromProgID(). I believe there is a same function in Python module win32com.

    2. Cast the server class to IRtdServer (Microsoft.Office.Interop.Excel.IRtdServer).

    3. Then one can use IRtdServer.ConnectData(topicID, topics, newData) to get the data:

      • topicID seems to be any random int;
      • topics is the parameters you used in Excel, it is an array;
      • newData is a bool, set it to be True if you request new data and not the cached data.
    4. However, I believe if one wants to get the data updated automatically, one would need a callback event.

    According to the linked blog post, the event class can also be got by progID. But I have not found any in the reg table.

    In C# I think it maybe possible to create a IRTDUpdateEvent. But I don't know how to deal with it with Python.

    Could someone help me work around these errors?