Skip to content
eoinomahon edited this page Jul 10, 2024 · 9 revisions

connect_SAP()

Connect to the SAP system and return the session object necessary for interaction.

Returns

  • session: object
    • The session object necessary to interact with SAP.

Description

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.

Example

session = connect_SAP()
# Use the session object to interact with SAP.

ObjectTree Class

__init__(self, session: object = None, date_format: str = '%m/%d/%Y')

Initialize the ObjectTree class.

Parameters

  • 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'.

Attributes

  • 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.

Raises

  • Exception
    • If failed to connect to SAP.

Notes

  • If session is not provided, a new connection to SAP is established using the connect_SAP function.
  • The info_retrieved_list contains the information to be retrieved from SAP objects.
  • The other attributes are initialized as empty dictionaries, lists, or None.

Example

object_tree = pysaprpa.ObjectTree(session, date_format='%d/%m/%Y')
# ObjectTree instance created with European date format.

start_transaction(self, t_code: str = None)

Start a new transaction in the SAP system using the specified T-code.

Parameters

  • t_code: str (optional)
    • The T-code to start the transaction with. Default is None.

Returns

  • ObjectTree
    • The updated instance of the class.

Description

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.

Notes

  • The method uses the StartTransaction method of the SAP session object to start the transaction. It then returns the updated instance of the class.

Example

object_tree.start_transaction('MB51')
# Tells SAP to go to parameter screen for MB51

end_transaction(self)

Returns

  • ObjectTree
    • The updated instance of the class.

Description

Ends the current transaction, clears object_tree, sap_fields_dict, etc... and returns to the menu.

Example

object_tree.end_transaction()

get_objects(self, window: int = 0) -> 'ObjectTree'

Retrieve the object tree from the SAP system using the specified window number and parse it to extract relevant field information.

Parameters

  • window: int (optional)
    • The window number to retrieve the object tree from. Default is 0.

Returns

  • ObjectTree
    • The updated instance of the class.

Description

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.

Example

object_tree.get_objects()
# Parsed and named SAP objects. Stored labels and object ids (where objects are) in sap_fields_dict

set_parameters(self, variant: str = '', **kwargs) -> 'ObjectTree'

Set Parameters for an ObjectTree Instance.

Parameters

  • variant: str (optional)
    • The variant to set.
  • **kwargs: dict
    • Additional parameters to set.

Kwargs Notes

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).

Returns

  • ObjectTree
    • The ObjectTree instance with the set parameters.

Example

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})

Example

execute(self, vkey: Union[int, str] = '') -> 'ObjectTree'

Execute a command in the SAP system.

Parameters

  • vkey: Union[int, str] (optional)
    • The virtual key to execute. Default is an empty string.

Returns

  • ObjectTree
    • The updated instance of the class.

Notes

  • After execution, the self.object_tree attribute is set to None. This is because execution often results in a new screen, and users must call get_objects() again to retrieve the updated objects.

Example

object_tree.execute()
# Told SAP to execute

export(self, how: str, directory: str, file_name: str) -> str

Export data from the SAP system using the specified export method and save it to the specified directory with the specified file name.

Parameters

  • how: str
    • The export method.
  • directory: str
    • The directory to export to.
  • file_name: str
    • The file name to export as.

Returns

  • str
    • The exported file path.

Notes

  • 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.

Example

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