SAP login self
import sys import win32com.client import win32api import win32gui import win32con # import win32ui import time import os import subprocess def Login(): sap_app = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe" subprocess.Popen(sap_app) time.sleep(1) flt = 0 while flt == 0: try: hwnd = win32gui.FindWindow(None, "SAP Logon 770") flt = win32gui.FindWindowEx(hwnd, None, "Edit", None) except: time.sleep(0.5) win32gui.SendMessage(flt, win32con.WM_SETTEXT, None, "BOT_LOGIN") win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0) win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0) time.sleep(0.1) dlg = win32gui.FindWindowEx(hwnd, None, "Button", None) # 登陆(0) win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0) win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0) SapGuiAuto = win32com.client.GetObject("SAPGUI") if not type(SapGuiAuto) == win32com.client.CDispatch: return application = SapGuiAuto.GetScriptingEngine if not type(application) == win32com.client.CDispatch: SapGuiAuto = None return connection = application.Children(0) if not type(connection) == win32com.client.CDispatch: application = None SapGuiAuto = None return time.sleep(2) flag = 0 while flag == 0: try: session = connection.Children(0) flag = 1 except: time.sleep(0.5) if not type(session) == win32com.client.CDispatch: connection = None application = None SapGuiAuto = None return
# SSO no need # session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "username" #此次放入您的SAP登陆用户名 # session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "password" #此次放入您的SAP登陆密码 session.findById("wnd[0]").sendVKey(0) return session def actions(session): """下面演示了使用mm03查看物料1000000000000的状态后再退回sap首页的过程""" session.findById("wnd[0]/tbar[0]/okcd").text = "mm03" session.findById("wnd[0]").sendVKey(0) session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "1000000000000" session.findById("wnd[0]").sendVKey(0) session.findById("wnd[1]").sendVKey(0) session.findById("wnd[0]/tbar[0]/okcd").text = "/n" session.findById("wnd[0]").sendVKey(0) # >Insert your SAP GUI Scripting code here< if __name__ == "__main__": session = Login() actions(session)