Python操作SAP
一些基础的操作SAP的Code
Open SAP
# Open SAP def open_sap(): try: Application(backend="win32").start(sap_path, timeout=60) log.info("Open SAP Successful") except: log.error("Open SAP Failed")
Enter AP1 and logon
# Enter AP1 then Logon def enter_sap_system(username, password, sid): try: # 登录AP1 (sid) log.info("Enter SAP system") app = Application(backend="win32").connect(path=sap_path) app.window(title='SAP Logon Pad 770').wait('exists ready', timeout=60) sap_window = app.window(title='SAP Logon Pad 770') sap_window.child_window(class_name="Edit").set_focus() sap_window.child_window(class_name="Edit").set_text(sid) sap_window.child_window(class_name="Edit").set_focus() send_keys("{ENTER}") time.sleep(2) sap_window.child_window(class_name="Edit").set_focus() sap_window.set_focus() send_keys("+{ENTER}") log.info("Enter sap system successful") session = "" time.sleep(2) # 连接SAP while True: try: pythoncom.CoInitialize() SapGuiAuto = win32com.client.GetObject("SAPGUI") application = SapGuiAuto.GetScriptingEngine if not type(application) == win32com.client.CDispatch: SapGuiAuto = None connection = application.Children(0) if not type(connection) == win32com.client.CDispatch: application = None SapGuiAuto = None session = connection.Children(0) if not type(session) == win32com.client.CDispatch: connection = None application = None SapGuiAuto = None break except: log.warning("Connect failed, please wait 3 seconds") time.sleep(3) # 登录 try: session.findById("wnd[0]/usr/txtRSYST-BNAME").text = username session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = password session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "EN" session.findById("wnd[0]/tbar[0]/btn[0]").press() except: log.warning("Login failed") # 登录冲突,踢人 try: session.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").select() session.findById("wnd[1]/tbar[0]/btn[0]").press() except: pass # 有时会弹出确认框,点击确认即可 try: time.sleep(3) session.findById("wnd[1]/tbar[0]/btn[0]").press() except: pass log.info("login successful, user:" + username) return session except: log.info("Enter SAP System failed")
退出SAP
def exit_after_n_second(n): time.sleep(n) # 关闭SAP __close_sap_window_by_class_name('SAP_FRONTEND_SESSION') __close_sap_window_by_title('SAP Logon Pad 770')
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)