找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
SAP亦橙网 首页 SAP技术 查看内容

Python程序操作SAP

2023-12-20 11:31| 发布者: 亦书| 查看: 94| 评论: 0

摘要: Python操作SAP一些基础的操作SAP的CodeOpen 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 ...
 

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

 


特别声明:文章或部分素材来源于网络,仅供SAPERP从业伙伴们交流学习使用,如果侵犯了您的权益,请联系网站管理人员删除!


路过

雷人

握手

鲜花

鸡蛋
上一篇:SAP CPI and PO下一篇:SAP SPOOL 打印过程
返回顶部