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.
In the past I wrote different blogs how to use Python in the context of ABAP:
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.
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.
Register this Python program as COM server.
Try the following VBScript to check if everything is okay.
The ABAP report do exactly the same as the VBScript.
Here the result of the ABAP report.
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
It tried this approach with Python 3.7.3 and Python for Windows 224 and it works only with x86. With Python 3.6.8, 3.7.2 and Python for Windows 224 all works well and as expected.