I am trying to establish connection to current SAP session with python using win32com. All the guides I have found so far are doing this with use of "saplogon.exe" but for our system, we can only login through Sap Logon Pad ("saplgpad.exe"). "saplogon.exe" is not present in any of the folders.
May someone please help me with the connection in my case?
Code using "saplogon.exe" looks as following:
import win32com.client
import sys
import subprocess
import time
# This function will Login to SAP from the SAP Logon window
def saplogin():
try:
path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
subprocess.Popen(path)
time.sleep(10)
SapGuiAuto = win32com.client.GetObject('SAPGUI')
if not type(SapGuiAuto) == win32com.client.CDispatch:
return
application = SapGuiAuto.GetScriptingEngine
if not type(application) == win32com.client.CDispatch:
SapGuiAuto = None
return
connection = application.OpenConnection("DCG210", True)
if not type(connection) == win32com.client.CDispatch:
application = None
SapGuiAuto = None
return
session = connection.Children(0)
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "USERNAME"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD"
session.findById("wnd[0]").sendVKey(0)
except:
print(sys.exc_info()[0])
finally:
session = None
connection = None
application = None
SapGuiAuto = None
saplogin()
The only thing working for me so far was login through shortcut but this way I wont get to session for purposes of further scripting:
import subprocess
# logs into SAP with auto choosing of PROD environment.
subprocess.check_call(['C:\Program Files (x86)\SAP\FrontEnd\SAPgui\\sapshcut.exe', '-system=PRD', '-client=010', '-user=XXXXXX', '-pw=XXXXXX'])
Is there maybe a way to combine these two approaches to get it working?
Thank you very much guys for any help you can provide.