Quantcast
Channel: Scripting Languages
Viewing all articles
Browse latest Browse all 68

How to use actual SAP NetWeaver RFC Library with Python - Execute ABAP Report

$
0
0

Hello community,

 

I presented until now in the series "How to use actual SAPNetWeaver RFC Library with Pyhton" :

 

Here now an example how to define and run ABAP code from Python.

All constants, structures and prototypes are in the quasi-include file sapnwrfc.py - you find it below.

The ABAP report itself is an array of strings. We use the function module (FM) RFC_ABAP_INSTALL_AND_RUN to implement and execute the ABAP program. We set the report line by line in the PROGRAM table of the FM, invoke the function and get a result back in the WRITES table. We read each line of ZEILE and add it to a Result string.

 

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

 

 

#-Include---------------------------------------------------------------
FileName = "sapnwrfc.py"
exec(compile(open(FileName).read(), FileName, "exec"))

 

#-Main------------------------------------------------------------------

 

#-Connection parameters-------------------------------------------------
RfcConnParams[0].name = "ASHOST"; RfcConnParams[0].value = "NSP"
RfcConnParams[1].name = "SYSNR" ; RfcConnParams[1].value = "00"
RfcConnParams[2].name = "CLIENT"; RfcConnParams[2].value = "001"
RfcConnParams[3].name = "USER"  ; RfcConnParams[3].value = "BCUSER"
RfcConnParams[4].name = "PASSWD"; RfcConnParams[4].value = "minisap"

 

#-ABAPReport------------------------------------------------------------
#-
#- Code your ABAP report here. The length of each line must be equal or
#- less than 72 characters.
#-
#-----------------------------------------------------------------------
ABAP=[]
ABAP.append("Report zTest Line-Size 256.")
ABAP.append("Write: 'Hello World from'.")
ABAP.append("Write: sy-sysid.")

 


hRFC = SAP.RfcOpenConnection(RfcConnParams, 5, RfcErrInf)
if hRFC != None:

 

  charBuffer = create_unicode_buffer(256 + 1)
  Result = ""

 

  hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "RFC_ABAP_INSTALL_AND_RUN", \
    RfcErrInf)

  if hFuncDesc != 0:
    hFunc = SAP.RfcCreateFunction(hFuncDesc, RfcErrInf)
    if hFunc != 0:

      hTable = c_void_p(0)

 

      #-Writes the report into the PROGRAM table------------------------
      if SAP.RfcGetTable(hFunc, "PROGRAM", hTable, RfcErrInf) == RFC_OK:
        for i in range(0, len(ABAP)):
          hRow = SAP.RfcAppendNewRow(hTable, RfcErrInf)
          rc = SAP.RfcSetChars(hRow, "LINE", ABAP[i], len(ABAP[i]), \
            RfcErrInf)

 

        if SAP.RfcInvoke(hRFC, hFunc, RfcErrInf) == RFC_OK:
          #-Gets the result from the WRITES table-----------------------
          if SAP.RfcGetTable(hFunc, "WRITES", hTable, RfcErrInf) == RFC_OK:
            RowCount = c_ulong(0)
            rc = SAP.RfcGetRowCount(hTable, RowCount, RfcErrInf)
            rc = SAP.RfcMoveToFirstRow(hTable, RfcErrInf)
            for i in range(0, RowCount.value):
              hRow = SAP.RfcGetCurrentRow(hTable, RfcErrInf)
              rc = SAP.RfcGetChars(hRow, "ZEILE", charBuffer, 256, \
                RfcErrInf)
              Result = Result + charBuffer.value
              if i < RowCount.value:
                rc = SAP.RfcMoveToNextRow(hTable, RfcErrInf)

 

            #-Shows the result------------------------------------------
            print(Result)

 

      rc = SAP.RfcDestroyFunction(hFunc, RfcErrInf)

 

  rc = SAP.RfcCloseConnection(hRFC, RfcErrInf)

 

else:
  print(RfcErrInf.key)
  print(RfcErrInf.message)

 

del SAP

 

#-End-------------------------------------------------------------------

 

As you can see, it is no problem to define and execute an ABAP program inside Python.

 

Enjoy it.

 

Cheers

Stefan

 

 

P.S. Here the file sapnwrfc.py - at the end of the post.


Viewing all articles
Browse latest Browse all 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>