ABAP Connectivity, SAP ABAP Development

How To Use Python Seamlessly Inside ABAP

Python is an interpreted, object-oriented, high-level programming language and it is used in many different application areas, e.g. in scientific and numeric computing. It can be very valuable and helpful to combine SAP ABAP with Python. In this blog I will describe a way to do that via the SAP GUI for Windows COM interface.

Here now another way. The communication between Python and ABAP runs in this case via the COM interface. Python offers via Python for Win32 (pywin32) extensions the possibility to create COM server. With ABAP and the SAP GUI for Windows is it possible to connect these COM server and to use its functions.

1. Download Python from here and install it.

ABAP Connectivity, SAP ABAP Development

2. Download the Python for Windows (pywin32) Extensions from here and install it.

ABAP Connectivity, SAP ABAP Development

3. Copy the following Python program to your computer.

# -*- coding: iso-8859-15 -*-
#-Begin—————————————————————–

class PythonDemo:
#To create a new GUID use the commands:
#>>> import pythoncom
#>>> print(pythoncom.CreateGuid())
_reg_clsid_ = “{D1B0A23F-0B6E-46D6-8880-744DBFFB86CD}”
_reg_progid_ = “Python.Demo”
_reg_desc_ = “Python Demo COM Server”
_public_methods_ = [“HelloWorld”, “HelloYou”, “SplitString”]

#-Function HelloWorld————————————————-
def HelloWorld(self):
import platform, struct
return “Hello World from Python ” + platform.python_version() + \
” on ” + platform.system() + ” (” + platform.architecture()[0] + \
“)”

#-Function HelloYou—————————————————
def HelloYou(self, name=”Anybody”):
return “Hello ” + str(name) + ” from Python”

#-Function SplitString————————————————
def SplitString(self, val, item=None):
import string
if item != None:
item = str(item)
return val.split(item)

#-Sub Main————————————————————–
def main():
import sys, win32com.server.register
if sys.argv[1].lower() == “–register”:
print(“Registering COM server…”)
if sys.argv[1].lower() == “–unregister”:
print(“Unregistering COM server…”)
win32com.server.register.UseCommandLine(PythonDemo)

#-Main——————————————————————
if __name__==”__main__”:
main()

#-End——————————————————————-​

This Python program contains a class with three different demo methods:

• HelloWorld returns a string with the using Python version, platform system and architecture.
• HelloYou returns a string with a parameter.
• SplitString – the standard example from the help – delivers a list of single words of a string.

Also the class contains different annotations for the COM registration.
The main routine of the program contains the registration code.

4. Register this Python program as COM server.

ABAP Connectivity, SAP ABAP Development

5. Try the following VBScript to check if everything is okay.

‘-Begin—————————————————————–

‘-Sub Main————————————————————
Sub Main()

Set PythonDemo = CreateObject(“Python.Demo”)

MsgBox PythonDemo.HelloWorld()

MsgBox PythonDemo.HelloYou(“Stefan”)

response = PythonDemo.SplitString(“Hello World”)
For Each item In response
MsgBox item
Next

End Sub

‘-Main—————————————————————-
Main

‘-End——————————————————————-​

This VBScript call from the COM server Python.Demo, in this case the Python program, the different methods – HelloWorld, HelloYou and SplitString.

Here the result from HelloWorld:

ABAP Connectivity, SAP ABAP Development

Here the result from HelloYou:

ABAP Connectivity, SAP ABAP Development

Hint: It does not matter which version of VBScript you use – x86 or x64 – it works with both.

6. Try the followin ABAP report.

“-Begin—————————————————————–
REPORT ZPYTHON.

DATA:
lo_python TYPE OLE2_OBJECT,
lv_return TYPE STRING,
lo_list TYPE OLE2_OBJECT
.

CREATE OBJECT lo_python ‘Python.Demo’.
CHECK sy-subrc = 0 AND lo_python-Handle > 0 AND lo_python-Type = ‘OLE2’.

CALL METHOD OF lo_python ‘HelloWorld’ = lv_return.
WRITE: / lv_return.

CALL METHOD OF lo_python ‘HelloYou’ = lv_return
EXPORTING
#1 = ‘Stefan’.
WRITE: / lv_return.

“Array is not supported as a data type for OLE parameters in ABAP
CALL METHOD OF lo_python ‘SplitString’ = lo_list
EXPORTING
#1 = ‘Hello World’.

“-End——————————————————————-​

The ABAP report do exactly the same as the VBScript.

ABAP Connectivity, SAP ABAP Development

Here the result of the ABAP report.

ABAP Connectivity, SAP ABAP Development

Hint: It is not possible to use a COM method which delivers a variant from the type array. So it is not possible to use the method SplitString – SAP note 633105.

At this point we see that is easy possible to use Python inside ABAP seamlessly via COM interface. We register the Python class as COM server and use it inside ABAP.

A huge advantage is that you can change the Python class on the fly. This means you can add or change methods without a new registration. So you can develop and test your Python class independently from your ABAP code, but you can use each change immediately in your ABAP code.

Hint: It could be possible that the security settings of your SAP GUI for Windows doesn’t allow to create an object as in my case. If it is so and you want to experiment with this approach, disable this settings temporary for the time of your experiment. And don’t forget to enable it after your experiment.

<26=Automation(Error) : ERROR OCCURED IN MODULE: [Python Demo COM Server]
<26=Automation(Error) : PROGRAM_ID |MODULE_NAME |METHOD_NAME |ERROR DESCRIPTION |VERSION |GUI VERSION |MODULE_PATH |
<26=Automation(Error) : Python.Demo |Python Demo COM Server |CREATEOBJECT |Creation of control “” denied! |Version info not found |7500.1.3.1138 |Module doesnot exist |
<26=Automation(Error) : “CreateObject”
#0: STRING “Python.Demo”
#1: LONG “1033”
could not create object

Leave a Reply

Your email address will not be published. Required fields are marked *