【问题标题】:Get data via DDE in Python by bypassing Excel在 Python 中绕过 Excel 通过 DDE 获取数据
【发布时间】:2015-03-08 20:24:30
【问题描述】:

我有一个数据提供程序,它提供一个 DDE 链接(我可以在 Excel 中使用)和一个在后台运行的用作数据管理器的 exe 文件(不确定这是否是所谓的 DDE 服务器)和DDE 链接连接到该 exe 文件。

我想绕过 Excel 并直接在 Python 中工作。我看到了一些关于 DDE 的示例,但它们都在 Python 2 中,而我正在使用 Python 3。

我在网上看到了这样的例子:

import win32ui
import dde
...
...
server = dde.CreateServer()
server.Create("SomeName")
...

但是这些示例显示了如何创建 DDE 服务器。就我而言,有一个现有的 exe 是数据管理器(DDE 服务器可能是?),在 Excel 中有一个菜单,我可以通过它获取数据,例如

' = DataProviderFunc1(Param1, Param2)'
' = DataProviderFunc2(Param1, Param2)'

我想用 Python 编写代码,直接获取 ' = DataProviderFunc1(Param1, Param2)' 等的输出,而不是打开 Excel 工作表,然后让 Python 从 Excel 工作表中读取输出。

这可能吗?

我正在使用 Python 3.4。谢谢

关于 DDE 模块的文档似乎很少,例如http://docs.activestate.com/activepython/2.4/pywin32/dde.html

【问题讨论】:

    标签: python-3.x pywin32 dde


    【解决方案1】:

    我找到的最接近文档的内容如下: client example, server example

    这些示例甚至没有评论,所以让我用一个评论的示例分享我的发现:

    import win32ui
    import dde
    
    #apparently "servers" talk to "servers"
    server = dde.CreateServer()
    #servers get names but I'm not sure what use this name
    #has if you're acting like a client
    server.Create("TestClient")  
    #use our server to start a conversation
    conversation = dde.CreateConversation(server)
    
    # RunAny is the server name, and RunAnyCommand is the topic
    conversation.ConnectTo("RunAny", "RunAnyCommand")
    # DoSomething is the command to execute
    conversation.Exec("DoSomething")
    # For my case I also needed the request function
    # request somedata and save the response in requested_data.
    requested_data = conversation.Request("somedata")
    

    关键函数似乎是 Exec 和 Request。两者都采用字符串,因此在您的特定情况下,您必须找出您的服务器想要什么。

    【讨论】:

      【解决方案2】:

      仅适用于使用 Interactive Brokers API 的开发人员:

      以下代码用于连接 TWS/IBAPI。

      注意:您需要保持 Excel 工作表打开并在其上运行 DDE 请求。

      import win32ui
      import dde
      import pandas as pd
      
      id = 100001 # the id you used in Excel to create the DDE request to an instrument
      req = 'askPrice' # can be any accepted by the excel wsheet
      server = dde.CreateServer()
      server.Create("ServerClient")
      DDEconversation = dde.CreateConversation(server)
      DDEconversation.ConnectTo("Stwsserver", "tick")
      s = f'{id}?{req}'
      value = pd.to_numeric(DDEconversation.Request(s), errors='coerce')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多