通过绕过Excel在Python中通过DDE获取数据

uda*_*day 7 pywin32 dde python-3.x

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

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

我在网上看到了一些例子:

import win32ui
import dde
...
...
server = dde.CreateServer()
server.Create("SomeName")
...
Run Code Online (Sandbox Code Playgroud)

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

' = DataProviderFunc1(Param1, Param2)'
' = DataProviderFunc2(Param1, Param2)'
Run Code Online (Sandbox Code Playgroud)

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

这可能吗?

我使用的是Python 3.4.谢谢

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

use*_*847 2

我发现的最接近文档的东西在这里: 客户端示例服务器示例

这些示例甚至没有注释,所以让我通过注释示例分享我的想法:

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")
Run Code Online (Sandbox Code Playgroud)

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