Friday, March 10, 2017

Pull SAP Client Options from saplogin.ini

Here is some code that allows you to pull the client options from the saplogin.ini file. This in itself isn't really all that useful but I'll show you a session selection module in a future post and in that I use this "getlogon" module to allow me to provide the user with a list of SAP client options such as PRD, QAS, or DEV.

def getsaplogonoptions():
    import ConfigParser
    Config = ConfigParser.ConfigParser()"C:\\SAP\saplogon.ini")

    def ConfigSectionMap(section):
        dict1 = {}
        options = Config.options(section)
        for option in options:
                dict1[option] = Config.get(section, option)
                if dict1[option] == -1:
                    DebugPrint("skip: %s" % option)
                print("exception on %s!" % option)
                dict1[option] = None
        return dict1

    Descriptions = ConfigSectionMap("Description")
    for i in Descriptions:
            name=str('item' + str(j))
    return loginlist

Note that ConfigParser can be installed via pip install configparser. Documentation for configparser is available here.

Wednesday, March 8, 2017

Verifying a SAP Object Exist

Good Morning All

Here's a little example of how you can verify if an SAP control exist before calling it. You can get creative with this but this is the base. Credit to my coworker Todd for initially coming up with this, a while back.

def verify(session=None, control=None):
    while True:
            return session.FindById(control)
            # Do something else here if needed

You could of course write this directly into your main script but having this as a separate module allows you to import it into your main script and then call it for each control as needed. Something like this.

from verify import verify
import win32com.client

SapGui = win32com.client.GetObject("SAPGUI").GetScriptingEngine
ses = SapGui.FindById("ses[0]")

material = 1234

ses.StartTransaction(Transaction="MM02")  # Start MM02 transaction
verify(ses, 'wnd[0]/usr/ctxtRMMG1-MATNR').text = material
verify(ses, 'wnd[0]/tbar[1]/btn[6]').Press()

The last two lines use the verify module. This will check if the control passed exist and if so preform the call on that control.

Wednesday, December 21, 2016

Using the Scripting Tracker

So here is a quick how to on the Scripting Tracker by Stefan Schnell. It is no long maintained unfortunately but still works well.

You can download the tracker here.

No need to install just download and extract the zip file. Then run Tracker.exe

Once you open your SAP session you can click the refresh button in the upper left of the scripting tracker window. It will populate the scripting objects from the currently open SAP GUI sessions.

You can now expand the node tree to see the various objects.

If you right click a scripting object from the tree it will be highlighted with a red box in the session.

You can then copy the ID or other info from the scripting tracker to use in your script.

There is also a recorder tab and a Scripting API tab that can be very helpful. The recorder is well a recorder just like the SAP builtin recorder. The Scripting API provides a list of available objects and methods and attributes in SAP for scripting. Note these are for VB and not Python so some syntax changes will be needed.

Also Stefan is not answer any questions related to the Scripting Tracker based on the site so I'd be happen to help in any way possible though I am by no means an expert. There also other tools available but Scripting Tracker is by far the best. You can also use the Script recorder built into SAP and then pull the objects from the vbs file.

Here is some sample code from those vbs recordings so you can compare with the Python code.

 If Not IsObject(application) Then  
   Set SapGuiAuto = GetObject("SAPGUI")  
   Set application = SapGuiAuto.GetScriptingEngine  
 End If  
 If Not IsObject(connection) Then  
   Set connection = application.Children(0)  
 End If  
 If Not IsObject(session) Then  
   Set session  = connection.Children(0)  
 End If  
 If IsObject(WScript) Then  
   WScript.ConnectObject session,   "on"  
   WScript.ConnectObject application, "on"  
 End If  
 session.findById("wnd[0]").resizeWorkingPane 88,25,false  
 session.findById("wnd[0]/tbar[0]/okcd").text = "vl03n"  
 session.findById("wnd[0]").sendVKey 0  
 session.findById("wnd[0]/usr/ctxtLIKP-VBELN").text = "xxxxxxxxxx"  
 session.findById("wnd[0]").sendVKey 0  
 session.findById("wnd[0]/usr/lbl[1,1]").caretPosition = 0  
 session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "testexport.csv"  
 session.findById("wnd[1]/usr/ctxtDY_FILE_ENCODING").text = "4110"  
 session.findById("wnd[1]/usr/ctxtDY_FILE_ENCODING").caretPosition = 4  

Monday, December 19, 2016

Basic SAP GUI Commands in Python

If you have ever done some scripting in VB for SAP GUI you some of this may look familiar. There are how ever a few differences. From what I've found these differences are also not well documented, at least anywhere I've found. Thus the reason for this blog.

So in this post as the title implies we'll be covering some of the basic commands for working with the SAP GUI. First you must be connected to use these so if you don't know how to do that see the post here.

This first one is self explanatory but let's break it down.


session is the variable we stored the SAP session connection data in when we connected to the SAP GUI. We'll be using this a lot so depending on the setup for your script you may want to make this a global variable so it doesn't have to be passed to your functions. Later on we'll look at more advanced ways to setup the connection where we put it in its own function and call it from our script.

You don't have to use functions for gui scripting you can just right flat code that executes from top to bottom but functions are very helpful for the same reason as in other programs.

The StartTransaction is used as you would guess to start a given SAP transaction in the given session. The StartTransaction function takes a single parameter the technical name of the transaction. You can use the "Transaction=" bit or not it is up to you. However the double or single quotas around the technical name are required. Basically is needs a string.

This next one is how you can execute a given command.

 session.FindById('wnd[0]/usr/ctxtLIKP-VBELN').text = deliverynumber  

This again uses session. The FindById is used to find a given gui object within the session. The string parameter that is passed is the ID of that gui object. I'll show you in another post how to find those IDs. The .text is the commend which is preformed on the given gui object. The text command can be used in both directions, which is to say you can either get the text of the gui object. You can also pass text to a gui object that accepts text like the above example. deliverynumber is just the value stored as a variable which we are passing.

One the VL03N screen we open with the StartTransaction command this would populate the Delivery field with the value passed. Note the value here can be a string or an int.

After you enter a delivery in transaction VL03N you would press enter or click the Enter button on the top toolbar. To do this we use the next command.


The only thing that changed here are the gui object ID and the command we execute on that gui object. As you can guess the Press() command clicks the gui object the same as with a single click of the left mouse button. However unlike keyboard and mouse scripting the mouse never moves so you can continue to use the computer while your script is running. You can even use another session of SAP along side the scripting.

So with this you have the very basics to start using SAP GUI scripting with Python. There is much we haven't covered but we'll get to it in later posts.

A great way to determine the commands needed to preform a given task is to use the VB script recorder built into SAP. It will have to be converted to Python syntax but it should get you started if your first script goes beyond what we have covered here.

Friday, December 16, 2016

Connecting Python to the SAP GUI

So I know you can script SAP GUI actions using Visual Basic. SAP even has a built in recorder but I don't use VB I use Python. I'm not going to debate the better language here that has been done. What I am going to do is provide some how to's on scripting SAP GUI with Python.

Python 2.7 to be exact but the same should work in Python 3.5. If you don't have Python installed you can get it here. Also I'll be using PyCharm Community edition but you can use any IDE you prefer. You can even use something like Notepad++ or plan old Notepad but I don't recommend Notepad as it has no syntax highlighting which will make it much easier.

Also I assume you have a working knowledge of SAP GUI usage and Python. If you don't know SAP you shouldn't be doing this. If you need some work on Python you can check out YouTube for many tutorials along with multiple websites.

For SAP I'll be using 740.


If you are on a different SAP version your controls used may be different.

So first things first as they say. To do anything with gui scripting you first have to connect to the SAP GUI scripting com object. In Python that is done using the win32com.client module.

 import win32com.client  

So now we can connect to an already open SAP GUI session. It only takes 2 lines of code. Real simple right?

 SapGui = win32com.client.GetObject("SAPGUI").GetScriptingEngine  
 session = SapGui.FindById("ses[0]")  

That's it. Given it won't do anything but it connects and then disconnects from your first open SAP session. You will know this by the spinning barber pole on the right of the SAP status bar.