cancel
Showing results for 
Search instead for 
Did you mean: 

PythonWin and SAPGUI Scripting

AB
Contributor
0 Kudos

I was hoping that somebody else might have posted some examples of SAPGUI scripting and Python(Win) - but I couldn't find any.

Here is a very small example of SAPGUI Scripting with PythonWin. It might serve as a quick start for others who might also share an interest in Python and GUIscripting.

In short - I've got so far as to get basic manipulation working.

import win32com.client

False, True = 0 , -1

app = win32com.client.Dispatch("Sapgui.ScriptingCtrl.1")

conn = app.OpenConnection("D47", True)

At this point the SAPGUI opens with the initial logon screen of the system. A connection has been created. I enter my user ID and password

>>> print conn.Id

returns /app/con[0]

>>> ses = app.FindById("ses[0]")

>>> ses.CreateSession() "opens up a new session Good!

>>> ses.SendCommand(Command="/nIW31") "starts Tx IW31

>>> ses.StartTransaction(Transaction="IW31") "start IW31 also

>>> window = ses.activeWindow

>>> print window.Id

/app/con[0]/ses[0]/wnd[0]

start SE38...

>>> field = window.FindByName(Name="RS38M-PROGRAMM", Type="GuiCTextField")

>>> print field.Id

/app/con[0]/ses[0]/wnd[0]/usr/ctxtRS38M-PROGRAMM

So - it seems that basic manipulation works OK

Next step is to try some useful...

I'll let you know how it goes..

Andrew

Message was edited by: Andrew Barnard

Incorporated successful code and removed some questions which I've now solved myself. (The successful code covers the questions!).

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Andrew,

I am having a problem with opening the SAP GUI using the python script.

I am using the below code,

import win32com.client

app = win32com.client.Dispatch("Sapgui.ScriptingCtrl.1")

conn = app.OpenConnection("EGD", True)

I am getting the error message 'Error 605,      The SAP GUI component could not instantiated.'

Can you please help me, as to how you could open the SAP GUI with the python script.

Regards,

Laxman

AB
Contributor
0 Kudos

Further to the above, I've now been able to translate and run VBS recordings within Python with only a few minor changes:

Here is the approach I've taken:

"// Some introductory setup..

import win32com.client

False, True = 0, -1

app = win32com.client.Dispatch("Sapgui.ScriptingCtrl.1")

con = app.OpenConnection("D47", True)

session = app.FindById("ses[0]")

"// Some input steps (reading from CSV files or other sources...

...

"// Then the actual GUI scripting steps..

Some "translation" hints:

(1) A VBS recording writes session.findById. In Python it is more accurately written as session.FindById

(2) A VBS statement such as session.findById("wnd[0]").resizeWorkingPane 132,25,false

would be written as session.FindById("wnd[0]").resizeWorkingPane( 132, 25, False)

similarly:

session.findById("wnd[0]").sendVKey 0 from VBS would be written session.FindById("wnd[0]").sendVKey( 0 )

Other than that - all seems pretty straight forward..

Former Member
0 Kudos

Hello,

I' have been wondering quite a while.  Now your posting got me started using Python to automate some SAP routine work.  Thank you.

Just one question.  How do you do translate this VBS line to python?

session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"

I see its value on python console. but could not figure how to assign a value to that.

Thanks.

Steve

Former Member
0 Kudos

You should try this.

obj = session.findById("wnd[0]/usr/txtRSYST-MANDT")

obj.text = "100"