随笔 - 12  文章 - 0  评论 - 0  阅读 - 5442

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')

 

posted on   Kyk  阅读(778)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示