-
Notifications
You must be signed in to change notification settings - Fork 1
Docs
Connect to the SAP system and return the session object necessary for interaction.
-
session: object- The session object necessary to interact with SAP.
This function establishes a connection to the SAP system using the SAP GUI scripting engine. It retrieves the session object, which is essential for further interactions with SAP.
session = connect_SAP()
# Use the session object to interact with SAP.Initialize the ObjectTree class.
-
session: object (optional)- The SAP session object. If not provided, a new connection to SAP is established.
-
date_format: str (optional)- The date format to be used. Default is '%m/%d/%Y'.
-
session: object- The SAP session object.
-
date_format: str- The date format used.
-
info_retrieved_list: list- List of information to be retrieved from SAP objects.
-
object_tree: None- The object tree retrieved from SAP.
-
sap_fields_dict: dict- Dictionary to store SAP fields.
-
vkey_map: dict- Dictionary to store virtual key mappings.
-
repeat_field_label_dict: dict- Dictionary to store repeat field labels.
-
object_left_label: list- List to store left labels of objects.
-
export_options_dict: dict- Dictionary to store export options.
-
Exception- If failed to connect to SAP.
- If
sessionis not provided, a new connection to SAP is established using theconnect_SAPfunction. - The
info_retrieved_listcontains the information to be retrieved from SAP objects. - The other attributes are initialized as empty dictionaries, lists, or None.
object_tree = pysaprpa.ObjectTree(session, date_format='%d/%m/%Y')
# ObjectTree instance created with European date format.Start a new transaction in the SAP system using the specified T-code.
-
t_code: str (optional)- The T-code to start the transaction with. Default is None.
-
ObjectTree- The updated instance of the class.
This method starts a new transaction in the SAP system using the specified T-code. If no T-code is provided, a ValueError is raised.
- The method uses the
StartTransactionmethod of the SAP session object to start the transaction. It then returns the updated instance of the class.
object_tree.start_transaction('MB51')
# Tells SAP to go to parameter screen for MB51-
ObjectTree- The updated instance of the class.
Ends the current transaction, clears object_tree, sap_fields_dict, etc... and returns to the menu.
object_tree.end_transaction()Retrieve the object tree from the SAP system using the specified window number and parse it to extract relevant field information.
-
window: int (optional)- The window number to retrieve the object tree from. Default is 0.
-
ObjectTree- The updated instance of the class.
This method retrieves the object tree from the SAP system using the specified window number. It then iterates through the object tree, extracting field labels and properties and storing them in instance variables. More information on how the labels are named/accessible in the set_parameters() documentation
The method returns the updated instance of the class, which contains the parsed object tree and relevant field information.
object_tree.get_objects()
# Parsed and named SAP objects. Stored labels and object ids (where objects are) in sap_fields_dictSet Parameters for an ObjectTree Instance.
-
variant: str (optional)- The variant to set.
-
**kwargs: dict- Additional parameters to set.
Dynamic Naming:
Labels/field names are created by cleaning object labels (by replacing special characters and replacing spaces with underscores), and appending input types to the end of the cleaned label:
-
Cleaned names:
- Máterial -> material
- Cómpany Code -> company_code
- Incl. all items -> incl_all_items
- Database -> database
- Morè Settings -> more_settings
-
Input types:
- Text Field: label_TEXT
- Button Field: label_BUTTON
- Radio Button: label_SELECT
- Flag: label_FLAG
- More: label_MORE
-
Combined:
- material_TEXT
- company_code_BUTTON
- incl_all_items_SELECT
- database_FLAG
- more_settings_MORE
Exception: Repeated Field Names:
When a field name is repeated, the library appends a frequency suffix to distinguish between occurrences. For example:
- First occurrence: sold_by_TEXT
- Second occurrence: sold_by_2_TEXT
- Third occurrence: sold_by_3_TEXT
- Etc…
Variants:
Variants determine how keyword arguments (kwargs) are treated. If a variant is given, user-passed kwargs take priority over variant values, but if a kwarg is not passed (omitted from the function call), the variant value remains unchanged. Thus, if a variant is given but a user passes a kwarg with a blank value, the blank value takes priority.
Acceptable Kwargs by Input Type:
-
_TEXT: Accepts either a string or a list of strings. -
_BUTTON: Accepts a list or a pandas Series. -
_SELECT: Accepts a boolean. -
_FLAG: Accepts a boolean. -
_MORE: Accepts a dictionary.
Special Case: Date Values:
- String date values must follow the format given in ObjectTree init.
- Date values can also be provided as tuples (month, year) in the format (MM: int, YYYY: int).
-
ObjectTree- The ObjectTree instance with the set parameters.
object_tree.set_parameters(cost_center_BUTTON=['100', '200', '300'],
posting_date_TEXT=(7, 2024),
layout_TEXT='/EOIN',
more_settings_MORE={'maximum_no_of_hits_TEXT': '999999','output_in_alv_grid_FLAG': True})Execute a command in the SAP system.
-
vkey: Union[int, str] (optional)- The virtual key to execute. Default is an empty string.
-
ObjectTree- The updated instance of the class.
- After execution, the
self.object_treeattribute is set toNone. This is because execution often results in a new screen, and users must callget_objects()again to retrieve the updated objects.
object_tree.execute()
# Told SAP to executeExport data from the SAP system using the specified export method and save it to the specified directory with the specified file name.
-
how: str- The export method.
-
directory: str- The directory to export to.
-
file_name: str- The file name to export as.
-
str- The exported file path.
- This function exports data from the SAP system using the specified export method and saves it to the specified directory with the specified file name.
object_tree.export(how='spreadsheet', directory='/fake/path', file_name='EXAMPLE.XLSX')
# Parsed and named SAP objects. Stored labels and object ids (where objects are) in sap_fields_dict