Code: Select all
from pyrfc import Connection
import pandas as pd
## Variables
sap_table = 'TABLENAME' # SAP Table Name
fields = ["FIELD1", "FIELD2"] # List of fields
options = [""] # the WHERE clause of the query is called "options"
max_rows = 10 # MaxRows
from_row = 0 # Row of data origination
try:
# Establish SAP RFC connection
conn = Connection(ashost='mysapserver.com', sysnr='00', client='000', user='username', passwd='password')
print(f"SAP Connection successful – connection object: {conn}")
if conn:
# Read SAP Table information
tables = conn.call("RFC_READ_TABLE", QUERY_TABLE=sap_table, DELIMITER='|', FIELDS=fields, OPTIONS=options, ROWCOUNT=max_rows, ROWSKIPS=from_row)
# Access specific row & column information from the SAP Data
data = tables["DATA"] # pull the data part of the result set
columns = tables["FIELDS"] # pull the field name part of the result set
df = pd.DataFrame(data, columns = columns)
if df:
print(f"Successfully extracted data from SAP using custom RFC - Printing the top 5 rows: {df.head(5)}")
else:
print("No data returned from the request. Please check database/schema details")
else:
print("Unable to connect with SAP. Please check connection details")
except Exception as e:
print(f"An exception occurred while connecting with SAP system: {e.args}")