├── sap_gui_1.vbs ├── sap_gui_1.py ├── sap_gui_2.py └── README.md /sap_gui_1.vbs: -------------------------------------------------------------------------------- 1 | 'Macro recorded with SAP "Script Recording and Playback..." 2 | 3 | If Not IsObject(application) Then 4 | Set SapGuiAuto = GetObject("SAPGUI") 5 | Set application = SapGuiAuto.GetScriptingEngine 6 | End If 7 | If Not IsObject(connection) Then 8 | Set connection = application.Children(0) 9 | End If 10 | If Not IsObject(session) Then 11 | Set session = connection.Children(0) 12 | End If 13 | If IsObject(WScript) Then 14 | WScript.ConnectObject session, "on" 15 | WScript.ConnectObject application, "on" 16 | End If 17 | session.findById("wnd[0]").resizeWorkingPane 152,30,false 18 | session.findById("wnd[0]/tbar[0]/okcd").text = "mm03" 19 | session.findById("wnd[0]").sendVKey 0 20 | session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "" 21 | session.findById("wnd[0]").sendVKey 0 22 | session.findById("wnd[1]").sendVKey 0 23 | session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP02").select 24 | session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP02/ssubTABFRA1:SAPLMGMM:2004/subSUB5:SAPLMGD1:2004/txtMARA-ZEINR").setFocus 25 | session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP02/ssubTABFRA1:SAPLMGMM:2004/subSUB5:SAPLMGD1:2004/txtMARA-ZEINR").caretPosition = 12 26 | session.findById("wnd[0]/tbar[0]/btn[15]").press 27 | -------------------------------------------------------------------------------- /sap_gui_1.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Simple example of SAP GUI Scripting via COM Objects and Interfaces 3 | """ 4 | 5 | 6 | import sys 7 | import win32com.client 8 | 9 | 10 | def main(): 11 | 12 | try: 13 | 14 | SapGuiAuto = win32com.client.GetObject("SAPGUI") 15 | if not isinstance(SapGuiAuto, win32com.client.CDispatch): 16 | return 17 | 18 | application = SapGuiAuto.GetScriptingEngine 19 | if not isinstance(application, win32com.client.CDispatch): 20 | SapGuiAuto = None 21 | return 22 | 23 | connection = application.Children(0) 24 | if not isinstance(connection, win32com.client.CDispatch): 25 | application = None 26 | SapGuiAuto = None 27 | return 28 | 29 | session = connection.Children(0) 30 | if not isinstance(session, win32com.client.CDispatch): 31 | connection = None 32 | application = None 33 | SapGuiAuto = None 34 | return 35 | 36 | # --- copied and changed VBS macro 37 | session.findById("wnd[0]/tbar[0]/okcd").text = "mm03" 38 | session.findById("wnd[0]").sendVKey(0) 39 | session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "" 40 | session.findById("wnd[0]").sendVKey(0) 41 | session.findById("wnd[1]").sendVKey(0) 42 | session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP02").select() 43 | document = session.findById( 44 | "wnd[0]/usr/tabsTABSPR1/tabpSP02/ssubTABFRA1:SAPLMGMM:2004/subSUB5:SAPLMGD1:2004/txtMARA-ZEINR" 45 | ).text 46 | print(document) 47 | session.findById("wnd[0]/tbar[0]/btn[15]").press() 48 | 49 | except: 50 | print(sys.exc_info()[0]) 51 | 52 | finally: 53 | session = None 54 | connection = None 55 | application = None 56 | SapGuiAuto = None 57 | 58 | 59 | if __name__ == "__main__": 60 | main() 61 | -------------------------------------------------------------------------------- /sap_gui_2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Simple example of SAP GUI Scripting via COM Objects and Interfaces 3 | """ 4 | 5 | 6 | import sys 7 | import win32com.client 8 | 9 | materials = ("", "", "") 10 | 11 | 12 | def main(): 13 | 14 | try: 15 | 16 | SapGuiAuto = win32com.client.GetObject("SAPGUI") 17 | if not isinstance(SapGuiAuto, win32com.client.CDispatch): 18 | return 19 | 20 | application = SapGuiAuto.GetScriptingEngine 21 | if not isinstance(application, win32com.client.CDispatch): 22 | SapGuiAuto = None 23 | return 24 | 25 | connection = application.Children(0) 26 | if not isinstance(connection, win32com.client.CDispatch): 27 | application = None 28 | SapGuiAuto = None 29 | return 30 | 31 | session = connection.Children(0) 32 | if not isinstance(session, win32com.client.CDispatch): 33 | connection = None 34 | application = None 35 | SapGuiAuto = None 36 | return 37 | 38 | for material in materials: 39 | # --- copied and changed VBS macro 40 | session.findById("wnd[0]/tbar[0]/okcd").text = "mm03" 41 | session.findById("wnd[0]").sendVKey(0) 42 | session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = material 43 | session.findById("wnd[0]").sendVKey(0) 44 | session.findById("wnd[1]").sendVKey(0) 45 | session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP02").select() 46 | document = session.findById( 47 | "wnd[0]/usr/tabsTABSPR1/tabpSP02/ssubTABFRA1:SAPLMGMM:2004/subSUB5:SAPLMGD1:2004/txtMARA-ZEINR" 48 | ).text 49 | print(document) 50 | session.findById("wnd[0]/tbar[0]/btn[15]").press() 51 | 52 | except: 53 | print(sys.exc_info()[0]) 54 | 55 | finally: 56 | session = None 57 | connection = None 58 | application = None 59 | SapGuiAuto = None 60 | 61 | 62 | if __name__ == "__main__": 63 | main() 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SAP and Python (SAP GUI Scripting, PyRFC) 2 | 3 | This is a collection of my notes and code snippets. 4 | I work with SAP on Windows 10 with SAP GUI for Windows 7.50. 5 | On my work account I don't have administrative rights and access to SAP NetWeaver RFC SDK. 6 | 7 | If you have questions, please contact me: 8 | * Email: {github account}@gmail (dot) com 9 | * LinkedIn: https://www.linkedin.com/in/alekczapski/ 10 | 11 | ## Table of contents 12 | 13 | * [Prequisities](#prequisities) 14 | * [Extracting Python 2.7 from MSI Windows Installer](#extracting-python-27-from-msi-windows-installer) 15 | * [Installing pip, setuptools, pywin32 (for SAP GUI Scripting)](#installing-pip-setuptools-pywin32-for-sap-gui-scripting) 16 | * [Installing PyRFC for Python 2.7 (for SAP RFC)](#installing-pyrfc-for-python-27-for-sap-rfc) 17 | * [Adding Python and _sapnwrfc.dll_ to PATH variable](#adding-python-and-sapnwrfcdll-to-path-variable) 18 | * [Related links](#related-links) 19 | * [SAP GUI Scripting](#sap-gui-scripting) 20 | * [Related links](#related-links-1) 21 | * [PyRFC](#parameters) 22 | * [Related links](#related-links-2) 23 | * [Similar topics](#similar-topics) 24 | * [Testing with Robot Framework and SapGui Library](#testing-with-robot-framework-and-sapgui-library) 25 | * [Related links](#related-links-3) 26 | 27 | ## Prequisities 28 | 29 | Because I don't have access to SAP NetWeaver RFC SDK, I am using a 32-bit _sapnwrfc.dll_ library provided with SAP GUI installation, which forces me to use a 32-bit version of PyRFC. PyRFC in compiled 32-bit version is only available for Python 2.7. 30 | 31 | ### Extracting Python 2.7 from MSI Windows Installer 32 | I install this way because a normal installation requires admin privileges. 33 | ```dos 34 | msiexec /a python-2.7.15.msi /qb TARGETDIR=C:\Python27 35 | ``` 36 | 37 | ### Installing pip, setuptools, pywin32 (for SAP GUI Scripting) 38 | ```dos 39 | C:\Python27\python.exe -m ensurepip --default-pip 40 | C:\Python27\python.exe -m pip install --upgrade pip setuptools 41 | C:\Python27\python.exe -m pip install --upgrade pywin32 42 | ``` 43 | 44 | ### Installing PyRFC for Python 2.7 (for SAP RFC) 45 | ```dos 46 | C:\Python27\Scripts\easy_install.exe https://github.com/SAP/PyRFC/raw/master/dist/pyrfc-1.9.3-py2.7-win32.egg 47 | ``` 48 | 49 | ### Adding Python and _sapnwrfc.dll_ to PATH variable 50 | 1. Press `Windows logo key + R` and run `rundll32 sysdm.cpl,EditEnvironmentVariables`. 51 | 1. Add `C:\Python27`, `C:\Python27\Scripts` and `C:\Windows\SysWOW64` to `PATH` variable. 52 | 53 | ### Related links 54 | https://blogs.sap.com/2017/05/15/sap-gui-for-windows-7.50-offers-sapnwrfc-library-version-7.49-pl-0/ 55 | 56 | ## SAP GUI Scripting 57 | 58 | ### Related links 59 | https://blogs.sap.com/2017/09/19/how-to-use-sap-gui-scripting-inside-python-programming-language/ 60 | https://blogs.sap.com/2014/11/20/scripting-tracker-development-tool-for-sap-gui-scripting/ 61 | https://www.stschnell.de/ 62 | https://sappython.blogspot.com/ 63 | https://stackoverflow.com/questions/26764978/using-win32com-with-multithreading 64 | https://mail.python.org/pipermail/python-win32/2008-June/007788.html 65 | 66 | ## PyRFC 67 | 68 | ### Related links 69 | http://wbarczynski.pl/calling-bapis-with-python-and-pyrfc/ 70 | https://blogs.sap.com/2016/02/21/how-to-use-actual-sap-netweaver-rfc-library-with-python-call-abap-report/ 71 | 72 | ## Similar topics 73 | 74 | ### Testing with Robot Framework and SapGui Library 75 | 76 | ### Related links 77 | https://pypi.org/project/robotframework-sapguilibrary/ 78 | https://frankvanderkuur.github.io/SapGuiLibrary.html 79 | --------------------------------------------------------------------------------