Python easy tutorial for SAP with SAP GUI script

Source: Internet
Author: User
Tags win32 sap gui



As we all know, if you want to use Python to do some desktop WIN32 application automation work, you need to use the famous Pywin32 especially the Win32com.client module, PYWIN32 installation can not be directly through the Pip install method, On Pywin32 's official GitHub link: https://github.com/mhammond/pywin32/releases. Select the version that corresponds to the desktop system version and Python version to install:






If everything is OK, the module will not be reported in Ipython when it is imported! Such as:



In [1]: Import win32com.client



The next pull is to establish a connection to the SAP GUI, as follows:


 
 
 1     SapGuiAuto = win32com.client.GetObject("SAPGUI")
 2     if not type(SapGuiAuto) == win32com.client.CDispatch:
 3       return
 4 
 5     application = SapGuiAuto.GetScriptingEngine
 6     if not type(application) == win32com.client.CDispatch:
 7       SapGuiAuto = None
 8       return
 9 
10     connection = application.Children(0)
11     if not type(connection) == win32com.client.CDispatch:
12       application = None
13       SapGuiAuto = None
14       return
15 
16     session = connection.Children(0)
17     if not type(session) == win32com.client.CDispatch:
18       connection = None
19       application = None
20       SapGuiAuto = None
21       return


The remainder of the code can be generated with the VBS script statement from the SAP native script playback and recording feature, directly into the Python code!






It should be emphasized that the script recording function does not record all mouse keyboard actions, if some windows are the operating system itself window, still need to combine the capture of Windows handle, combined with SendMessage, PostMessage and other Win32 API functions to handle.



Of course, you also need to consult the SAP GUI script Help file, which will list all the sap underlying object properties, methods, corresponding parameter type, quantity, etc., the interface is as follows:






In one of my actual work, I need to get a shell form that resembles Excel cell value, the table is ALV format, but the actual recording can not record to the cell value, perhaps you racked your brains, and finally through other methods (such as with Sendkey Ctrl + Y,ctrl +) C) has achieved the same effect, but I still recommend using SAP script native API to solve. By looking at the SAP GUI, you know that it belongs to "Guigridview Object" and has the method ' Getcellvalue method ' as follows:


Public Function GetCellValue( _
   ByVal Row As Long, _
   ByVal Column As String _
) As String


Where the column parameter is string type, by recording the SAP script, double-clicking the corresponding can record to the column name, usually recording code similar to:


Session.findbyid ("wnd[0]/usr/cntlctrl_containerbseg/shellcont/shell""  Sgtxt"


So if you want to get a value for a cell (called a cell in SAP), the syntax is as follows:


" Sgtxt ")


It should be noted that the ALV format of SAP bottom line number is starting from 0, if you want to know how many rows of data in the table, two lines of code can be done (the ID of the table is recorded by the script can be obtained):


 
Set Table = session.findById("wnd[0]/usr/cntlCTRL_CONTAINERBSEG/shellcont/shell")
       
tableRowCount = Table.RowCount


If you want to get the SAP window title to assist the program to judge, the syntax is very simple, directly invoke the Session object's Text property, such as:


Window_caption=session.findbyid ("wnd[0]"). Text





The operation of these property methods seems simple, but if you do not consult the relevant SAP GUI script API documentation, do not understand the API, you are difficult to experiment with yourself, perhaps reluctantly implemented in other ways, but inevitably go a detour or sacrifice stability. After all, neither the VB nor the Python compiler will code hints and automatically supplement the SAP underlying API. So when necessary, be sure to check out the Help file.



A prominent blogger at the SAP blog site is recommended here: Stefan Schnell ( He wrote a lot of SAP scripts in conjunction with other languages ) and the free SAP scripting tools he developed:


Scripting tracker–development Tool for SAP GUI Scripting, with a blog link:https://blogs.sap.com/2014/11/20/scripting-tracker-development-tool-for-sap-gui-scripting/


Download link containing scripting Tracker:https://tracker.stschnell.de/



The tool is more visual and easy to use than native SAP script, where the Analyser module interface is based on the ability to clearly capture the tree structure of the SAP interface and the corresponding element ID attributes:






You care about the script Recorder recorder long this way, it supports recording scripts and supports the export of various scripting languages such as VB, Python, Java, PowerShell, and more:






Really is a conscience application, highly recommended!



With these, it is no longer difficult to operate SAP with Python control!



Here is a simple example of a Python operation that opens T-code "mm03" for reference:


 
 1 #-Begin-----------------------------------------------------------------
 2 
 3 #-Includes--------------------------------------------------------------
 4 import sys, win32com.client
 5 
 6 #-Sub Main--------------------------------------------------------------
 7 def Main():
 8 
 9   try:
10 
11     SapGuiAuto = win32com.client.GetObject("SAPGUI")
12     if not type(SapGuiAuto) == win32com.client.CDispatch:
13       return
14 
15     application = SapGuiAuto.GetScriptingEngine
16     if not type(application) == win32com.client.CDispatch:
17       SapGuiAuto = None
18       return
19 
20     connection = application.Children(0)
21     if not type(connection) == win32com.client.CDispatch:
22       application = None
23       SapGuiAuto = None
24       return
25 
26     session = connection.Children(0)
27     if not type(session) == win32com.client.CDispatch:
28       connection = None
29       application = None
30       SapGuiAuto = None
31       return
32 
33 
34     #session.findById("wnd[0]").resizeWorkingPane(65, 19, 0)
35     session.findById("wnd[0]/tbar[0]/okcd").text = "mm03"
36     session.findById("wnd[0]").sendVKey(0)
37     session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text="9000000000012"
38     session.findById("wnd[0]").sendVKey(0)
39     session.findById("wnd[1]/tbar[0]/btn[0]").press()
40     session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP02").select()
41 
42   except:
43     print(sys.exc_info()[0])
44 
45   finally:
46     session = None
47     connection = None
48     application = None
49     SapGuiAuto = None
50 
51 #-Main------------------------------------------------------------------
52 if __name__ == "__main__":
53   Main()
54 
55 #-End-------------------------------------------------------------------


Ps:1, the actual SAP script recording process, will record a large number of programs such as Setfocus,caretposition,resizeworkingpane, such as no practical help to the program, in order to improve the efficiency of program execution, it is recommended that the recorded script statements are properly annotated and deleted;



2, this part of the SAP Script API statement with VB, needs to be modified to apply to Python.






Python easy tutorial for SAP with SAP GUI script


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.