Python and SAP RFC Connections

Firstly download and install the latest stable version of Python. Once done , then download and install the Python library pysaprfc. Copy pysap.py somewhere in your PYTHONPATH (site-packages for example). Then you can then communicate with your SAP instance utilising code like this.

# Retrieve data from SAP/R3
import pysap

# Variables
WhereSQL    = "IDOCTYP like '%Z_CONN%'"
SQLTable    = "EDIFCT" 
MaxRows     = "5000"
CONN_FILE   = "C:/Python26/pysaprfc/examples/sapconn.ini"
CONN_NAME   = "SAPSANDBOX"

# Change next line to be able to connect to your SAP system
sap_conn=pysap.Rfc_connection(conn_file=CONN_FILE,conn_name=CONN_NAME)
sap_conn.open()

# Read entries from SQLTable 
itab=sap_conn.read_table(SQLTable,options=[WhereSQL],max_rows=MaxRows)
# Print some fields from itab to show it worked
for p in itab:
 print '%(FCTNAM)s %(FCTVRS)s %(IDOCTYP)s %(CIMTYP)s %(MESTYP)s %(MESCOD)s %(MESFCT)s %(OBJNAM)s %(DIRECT)s' % p

sap_conn.close()