• python使用SAP GUI操作SAP的几个坑


    1) 首先必须设定SAP中设置可以使用SAP GUI Script.

    详细参见以下文章:

        https://blog.csdn.net/weixin_44447687/article/details/106216776

    简单来说就是 SAP ==>RZ11==>sapgui/user_scripting==>需要设定为true

           需要设定为  启用脚本==>下面的两个警告勾勾去掉(否则会出现不需要的弹窗)

    2) 需要安装pywin32

       pip install pywin32
      参照
      https://blog.csdn.net/tq501501/article/details/119425042

    3) 录制Script

    4) Python基本代码如下

      

    #-Begin-----------------------------------------------------------------
    #-Includes--------------------------------------------------------------
    import sys, win32com.client
    import win32api,win32gui,win32con,win32ui,time,os,subprocess
    #-Sub Main--------------------------------------------------------------
    def Main():

        sap_app = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe" #您的saplogon程序本地完整路径
        subprocess.Popen(sap_app)
        time.sleep(1)
        flt=0
        while flt==0:
            try:
                hwnd = win32gui.FindWindow(None,"SAP Logon 740")
                flt=win32gui.FindWindowEx(hwnd,None,"Edit", None)  #capture handle of filter
            except:
                time.sleep(0.5)
        win32gui.SendMessage(flt,win32con.WM_SETTEXT,None,"DEV")
        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)

        session = connection.Children(0)
       
        if not type(session) == win32com.client.CDispatch:
            connection = None
            application = None
            SapGuiAuto = None
            return
        print("OK")

        session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "XXXXXX" #此次放入您的SAP登陆用户名
        session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "xxxxxx"  #此次放入您的SAP登陆密码
        session.findById("wnd[0]").sendVKey(0)
        """下面演示了使用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 = "180100069380"
        session.findById("wnd[0]").sendVKey(0)
        #session.findById("wnd[0]/tbar[0]/okcd").text = "/n"
        #session.findById("wnd[0]").sendVKey(0)
        #session.findById("wnd[0]/tbar[0]/btn[3]").press()
        #session.findById("wnd[0]/tbar[0]/btn[3]").press()
        #>Insert your SAP GUI Scripting code here<
    #-Main------------------------------------------------------------------
    if __name__ == "__main__":
      Main()
    #-End-------------------------------------------------------------------
    参考资料
    https://blog.csdn.net/tq501501/article/details/119425042 安装
    https://blog.csdn.net/zhichuan0307/article/details/107205883 设定
    https://www.cnblogs.com/new-june/p/NewJune.html python代码
    https://blog.csdn.net/weixin_44447687/article/details/106216776 设定

  • 相关阅读:
    最短路径 Floyd && spfa
    queue 优先队列
    POJ Wormholes 最短路径 ballman_ ford 有负环
    上帝造题五分钟
    算法提高 新建Microsoft Word文档
    算法训练 最短路
    Ubuntu14 Could not open file /var/lib/dpkg/status
    MongoDB权威指南<2> 1-1 MongoDB 介绍
    SaltStack Char03 实践案例
    ELK Stack
  • 原文地址:https://www.cnblogs.com/wonder223/p/16382032.html
Copyright © 2020-2023  润新知