-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (50 loc) · 1.66 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import logging
from sap_gui import SapGui
from typing import Dict, Optional
def get_sap_config() -> Dict[str, str]:
"""Return SAP configuration parameters."""
return {
"platform": "PRD", # SAP system identifier
"client": "110", # Client number
"username": "USER",
"password": "PASSWORD",
"language": "PT" # Language code
}
def create_sales_order(sap: SapGui) -> Optional[str]:
"""Example function to create a sales order in SAP."""
try:
# Navigate to VA01 transaction
sap.perform_operation("/nVA01", "wnd[0]/usr/ctxtVBAK-AUART")
input("Press Enter after VA01 transaction is open...")
# Additional order creation logic...
return "Order created successfully"
except Exception as e:
logging.error(f"Failed to create sales order: {str(e)}")
return None
def main():
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Initialize SAP session
try:
sap = SapGui(get_sap_config())
# Login to SAP
if not sap.sapLogin():
raise Exception("Failed to login to SAP")
# Perform business operations
result = create_sales_order(sap)
if result:
logging.info(result)
except Exception as e:
logging.error(f"SAP automation failed: {str(e)}")
finally:
# Ensure proper logout and cleanup
try:
sap.sapLogout()
sap.close_connection()
except Exception as e:
logging.error(f"Cleanup failed: {str(e)}")
if __name__ == "__main__":
main()