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.StartTransaction(Transaction="VL03N")  

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.

 session.FindById('wnd[0]/tbar[0]/btn[0]').Press()  

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.

10 comments:

  1. Thanks for publishing this!! I have been trying to move my scripts from VBA to Python for some time. It is great to finally find documentation!

    ReplyDelete
  2. Hi Jason,

    Please enable code formatting for replies for the rest of us, because we cannot contribute clean answers without it.

    Thanks!

    ReplyDelete
  3. How to get Text from text box or from message using python and SAP GUI.

    ReplyDelete
    Replies
    1. use the .text method will return a string with the text. Example:
      session.FindById(/app/con[0]/ses[0]/wnd[0]/usr/ctxtLIKP-VBELN).text = 1234567890

      This would set the delivery field in VL03N to 1234567890 and the same can be done to pull data from the field.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi Jason,
    I am trying to create a sales order VA01 TCode using SAP-Python , but i got stuck where i need to enter matrial and quantity in grid .

    My question is how to handle Sap GUI Grid to Enter Material and quantity.
    I need to click on first row and enter Material using python , I tried to perform like this , session.FindById("ses[0]/wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1,1]").text = "M0097987"

    i am getting following error:

    line 18, in session.FindById("ses[0]/wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1,1]").text = "MM0001" File "", line 3, in FindById pywintypes.com_error: (-2147352567, 'Exception occurred.', (619, 'SAP Frontend Server', 'The control could not be found by id.', 'C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\sapfront.HLP', 393215, 0), None) Process finished with exit code 1

    Please help me on this, how i can handle grid using python for sap gui scripting?

    ReplyDelete
  6. Hi try this.
    This sets the material but you can change as needed for other fields for your case. The issue is with your \. In python \ is an escape character so use \\ to prevent it.

    session.FindById('ses[0]/wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1,1]').text = i[0]

    The use the below before going to next item.

    session.FindById('ses[0]/wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_POAN').Press()

    Haven't tested this today but it is from working example so should be good.

    ReplyDelete
  7. Hi Jason,
    how i ca use this "..hell[1]").selectItem " 2","&Hierarchy" in SAP-Python Please help me on this

    ReplyDelete
  8. session.FindById('ses[0]/wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_POAN').Press()

    this is not working

    ReplyDelete
  9. hi jason, to write text I use session.FindById('wnd[0]/usr/ctxtLIKP-VBELN').text="12345"

    to get text it gives me an error with this syntax

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

    ReplyDelete