A professional Python library for automating SAP operations via SAP GUI Scripting. PySAPManager provides an object-oriented API to automate SAP tasks from Python by leveraging the SAP GUI Scripting engine.
PySAPManager enables you to:
- Automate startup and supervision of the SAP Logon process
- Open and manage target SAP connections
- Create and retrieve SAP sessions
- Execute VBScript scripts directly recorded from SAP GUI (both synchronously and asynchronously)
- Orchestrate parallel script executions while respecting COM and SAP GUI constraints
- Handle multi-threaded automation workflows
- Work with Excel files within automated scripts
A static HTML file was generated with pdoc.
View the documentation here.
- Windows environment with COM support
- SAP GUI installed on the workstation
- SAP GUI Scripting enabled on the client side
- SAP GUI Scripting authorized on the server side
Install the latest version from PyPI:
In the future...
Or install from source:
git clone https://github.com/ignYoqzii/pysapmanager.git
cd pysapmanager
pip install -e .psutil- Cross-platform process managementpywin32- Windows COM interface support
These are automatically installed with the package.
from pysapmanager import SAPManager, SAPConfig, ScriptExecutionRequest
# Configure SAP connection
config = SAPConfig(
sap_logon_path=r"C:\Program Files (x86)\SAP\FrontEnd\SAPGUI\saplogon.exe",
connection_description="your_connection_name",
)
# Use context manager for automatic cleanup
with SAPManager(config=config) as manager:
application = manager.get_application()
connection = application.open_connection()
session = connection.first_session()
# Execute a script
output = session.execute_script(
ScriptExecutionRequest(
script_path=r"C:\path\to\your_script.vbs",
creates_excel=True,
created_file_path=r"C:\path\to\output.xlsx",
)
)For parallel script execution across multiple sessions:
from pysapmanager import SAPManager, SAPConfig, ScriptExecutionRequest, com_initialized
config = SAPConfig(
sap_logon_path=r"C:\Program Files (x86)\SAP\FrontEnd\SAPGUI\saplogon.exe",
connection_description="your_connection_name",
)
# Use com_initialized() for multi-threaded environments
with com_initialized():
with SAPManager(config=config, max_parallel_scripts=2) as manager:
application = manager.get_application()
connection = application.open_connection()
# Create multiple sessions
session1 = connection.first_session()
session2 = connection.create_session()
# Submit scripts asynchronously
task1 = session1.execute_script_async(
ScriptExecutionRequest(
script_path=r"C:\path\to\script1.vbs",
creates_excel=True,
created_file_path=r"C:\path\to\output1.xlsx",
)
)
task2 = session2.execute_script_async(
ScriptExecutionRequest(
script_path=r"C:\path\to\script2.vbs",
creates_excel=True,
created_file_path=r"C:\path\to\output2.xlsx",
)
)
# Wait for all scripts to complete
manager.wait_all(task1, task2)SAPManager
Main facade for SAP automation. Handles SAP Logon startup, connection management, and script execution orchestration.
get_application()- Access SAP GUI Scripting applicationexecute_script()- Execute a script synchronouslyexecute_script_async()- Execute a script asynchronouslywait_all(*tasks)- Wait for all async tasks to complete
SAPConfig
Configuration for SAP connections.
sap_logon_path- Path to saplogon.execonnection_description- SAP connection description- Additional optional parameters for advanced configurations
SAPApplication
Access to SAP GUI Scripting application interface.
open_connection()- Open a SAP connectionclose()- Close the application
SAPConnection
Represents an open SAP connection.
first_session()- Get the first available sessioncreate_session()- Create a new session in the connectionclose()- Close the connection
SAPSession
Represents a SAP session for script execution.
execute_script(request)- Execute a script synchronouslyexecute_script_async(request)- Execute a script asynchronously
ScriptExecutionRequest
Parameters for script execution.
script_path- Path to the VBScript filecreates_excel- Whether the script creates an Excel filecreated_file_path- Path where the output Excel file will be saved
com_initialized()
Context manager for proper COM initialization in multi-threaded environments.
with com_initialized():
# COM-dependent code here
passset_logging_enabled(enabled)
Globally enable or disable all library logging.
from pysapmanager import set_logging_enabled
set_logging_enabled(False) # Silence all PySAPManager log outputis_logging_enabled()
Check the current global logging state.
from pysapmanager import is_logging_enabled
if is_logging_enabled():
print("Logging is enabled")By default, PySAPManager logs status messages via a console logger. To silence all output:
from pysapmanager import set_logging_enabled
set_logging_enabled(False)Or pass the enable_logging parameter to SAPManager:
with SAPManager(config=config, enable_logging=False) as manager:
# No logging output during this block
passFor automated login with credentials:
from pysapmanager import SAPConfig, SAPCredentials
credentials = SAPCredentials(
username="your_username",
password="your_password",
)
config = SAPConfig(
sap_logon_path=r"C:\Program Files (x86)\SAP\FrontEnd\SAPGUI\saplogon.exe",
connection_description="your_connection",
credentials=credentials,
)- Type Hints - Full type annotations for IDE support and type checking
- Error Handling - Comprehensive exception hierarchy for easy error management
- Context Managers - Automatic resource cleanup with
withstatements - Async Execution - Non-blocking script execution with parallel control
- COM Support - Proper handling of COM in multi-threaded environments
- Process Management - Automatic SAP Logon startup and supervision
- Plug and Play - Easily use pre-recorded scripts from SAP with the manager
The library provides specific exceptions for different error scenarios:
from pysapmanager import SAPManager, SAPConfig
config = SAPConfig(...)
try:
with SAPManager(config=config) as manager:
# Your SAP automation code
pass
except Exception as e:
print(f"SAP automation failed: {e}")-
Always use context managers - Ensures proper cleanup of SAP connections and processes
with SAPManager(config=config) as manager: # Your code here
-
Use
com_initialized()for multi-threaded code - Required for proper COM initialization in threadswith com_initialized(): with SAPManager(config=config) as manager: # Your multi-threaded code
-
Set realistic parallelism limits - Don't exceed your SAP system's capacity and SAP session's limit
manager = SAPManager(config=config, max_parallel_scripts=2)
-
Handle async tasks properly - Always wait for async tasks before closing the manager
task = session.execute_script_async(request) manager.wait_all(task)
-
Store credentials securely - Never hardcode credentials; use environment variables or secure vaults
import os username = os.getenv("SAP_USERNAME") password = os.getenv("SAP_PASSWORD")
If you get an error about SAP GUI Scripting not being available:
-
Verify SAP GUI Scripting is enabled on your client
-
Ensure SAP GUI Scripting is authorized on the server side
- Contact your SAP system administrator
Always wrap multi-threaded code with com_initialized():
from pysapmanager import com_initialized
with com_initialized():
# Your multi-threaded SAP automation code
passIncrease timeout values in ScriptExecutionRequest() if your script takes longer to finish:
request = ScriptExecutionRequest(
script_path=r"Path\to\your\script.vbs",
timeout=180, # 3 minutes
)This project is licensed under the MIT License - see the LICENSE file for details.
This library is not affiliated with or endorsed by SAP SE. SAP and all its products are trademarks or registered trademarks of SAP SE in Germany and other countries.