python 连接SAP 代码
阅读原文时间:2023年07月08日阅读:3

def Main():
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 750")
flt = win32gui.FindWindowEx(hwnd, None, "Edit", None)
except Exception as e:
time.sleep(0.5)

win32gui.SendMessage(flt, win32con.WM\_SETTEXT, None, "IR2-ERP Integration UAT")  
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  
session.findById("wnd\[0\]/usr/txtRSYST-BNAME").text = "WANG"  # 此次放入您的SAP登陆用户名  
session.findById("wnd\[0\]/usr/pwdRSYST-BCODE").text = "Wxz"  # 此次放入您的SAP登陆密码  
session.findById("wnd\[0\]").sendVKey(0)

if __name__ == "__main__":
Main()

标黄的地方是需要自己改的

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章