Get data via DDE in Python by bypassing Excel
Asked Answered
H

2

13

I have a data provider who provides a DDE link (that I can use in Excel) and an exe file that runs in the background which serves as a Data Manager (not sure if this is what called as a DDE Server) and the DDE link connects to that exe file.

I want to bypass Excel and work directly in Python. I saw some examples about DDE but they were all in Python 2 and I am using Python 3.

I saw examples on the net that do something like:

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

But these examples show how to create a DDE server. In my case, there is an existing exe that is the data manager (DDE server may be?) and within Excel there is a menu via which I can get data such as

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

I want to write a code in Python that directly gets output of ' = DataProviderFunc1(Param1, Param2)' etc., instead of having an Excel sheet open and then letting Python read the output from the Excel sheet.

Is this possible?

I am using Python 3.4. Thanks

There seems to be very little document on the DDE module, e.g. http://docs.activestate.com/activepython/2.4/pywin32/dde.html

Hod answered 8/3, 2015 at 20:24 Comment(1)
how did you find out details about the server using the excel file?Clinkerbuilt
M
2

The closest things to documentation that I've found are here: client example, server example

The examples aren't even commented so let me share what I've figured out with a commented 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")

Key functions seem to be Exec and Request. Both take strings so in your particular case you'll have to find out what your server wants.

Maidenhead answered 1/8, 2017 at 23:39 Comment(1)
how did you find out details about the server and what its doing using the excel file?Clinkerbuilt
A
0

Only for developers working with Interactive Brokers API:

The code below worked to connect with TWS/IBAPI.

Note: you need to leave the Excel sheet open and the DDE requests running on it.

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')
Aleph answered 13/5, 2021 at 13:13 Comment(1)
how did you find out details about the server using the excel file?Clinkerbuilt

© 2022 - 2024 — McMap. All rights reserved.