Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
The programming language Python offers many possibilities. Also it supports with py32win ActiveX automation, and therefore SAP GUI Scripting. The following example shows, how easy it is to take the recorded VBScript code and to use it inside Python.

In the comment lines you see the recorded VBScript code as a direct comparison. Here an example which opens TAC SE16, enters table TADIR and execute the TAC:
#-Begin-----------------------------------------------------------------

#-Includes--------------------------------------------------------------
import sys, win32com.client

#-Sub Main--------------------------------------------------------------
def Main():

try:

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.Children(0)
if not type(connection) == win32com.client.CDispatch:
application = None
SapGuiAuto = None
return

session = connection.Children(1)
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return

#session.findById("wnd[0]").resizeWorkingPane 173, 36, 0
session.findById("wnd[0]").resizeWorkingPane(173, 36, 0)
#session.findById("wnd[0]/tbar[0]/okcd").text = "/nse16"
session.findById("wnd[0]/tbar[0]/okcd").text = "/nse16"
#session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey(0)
#session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
#session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey(0)
#session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press()

except:
print(sys.exc_info()[0])

finally:
session = None
connection = None
application = None
SapGuiAuto = None

#-Main------------------------------------------------------------------
if __name__ == "__main__":
Main()

#-End-------------------------------------------------------------------

 

And after all we see that it is very easy to use SAP GUI Scripting inside Python. The recorded VBScript code can use almost always unchanged, sometimes it is necessary to set some brackets.

As IDE I use VS Code with Python extension, here a look at it:

43 Comments
Labels in this area