How to work around `pywintypes.com_error` using pyrtd/pythoncom?
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
Comments
-
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 forRTDClient()
) 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#:
- A C# client for Excel real-time data (RTD) servers
- Excel RTD Client in C#
- http://bbs.csdn.net/topics/320252117
- Excel RTD Servers: Multiple Topics in C#
The basic idea is:
-
Get the class type of rtd server from register table by
ProgID
(for example,RTDTime.RTD
, you can find them inHKEY_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 modulewin32com
. Cast the server class to
IRtdServer
(Microsoft.Office.Interop.Excel.IRtdServer
).-
Then one can use
IRtdServer.ConnectData(topicID, topics, newData)
to get the data:-
topicID
seems to be any randomint
; -
topics
is the parameters you used in Excel, it is an array; -
newData
is abool
, set it to beTrue
if you request new data and not the cached data.
-
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?