[Python] class 'pywintypes.com_error' for running a SAP transaction

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use a script in Python (Spyder) to open SAP and run a transaction. to acheive this goal I recorded SAP vbscript and with couple of tweaks for Python usage it works fine until it gets to the middle of SAP script and it triggers the class 'pywintypes.com_error' ! The code I use to do this task is following. Any help on how to get around this issue is highly appreciated:

    def saplogin():

    try:

    path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
    subprocess.Popen(path)
    time.sleep(2)

    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.OpenConnection("SAP")

    if not type(connection) == win32com.client.CDispatch:
    application = None
    SapGuiAuto = None
    return

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

    session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "Uname"
    session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "Pass"
    session.findById("wnd[0]").sendVKey(0)




    session.findById("wnd[0]").resizeWorkingPane (98,16,False)
    session.findById("wnd[0]/tbar[0]/okcd").text = "S032"
    session.findById("wnd[0]").sendVKey (0)
    session.findById("wnd[0]/usr/radP_MATL").select
    session.findById("wnd[0]/usr/chkP_LTPC").selected = False
    session.findById("wnd[0]/usr/ctxtS_FEVOR-LOW").text = "86A"
    session.findById("wnd[0]/usr/radP_SORT3").setFocus
    session.findById("wnd[0]/usr/radP_SORT3").select
    session.findById("wnd[0]/tbar[1]/btn[8]").press
    session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[2]").select
    session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select
    session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/usr/ctxtDY_PATH").text = "D:\PowerBIKhodam\WIP_Status\86A"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "S32.xls"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 12
    session.findById("wnd[1]/tbar[0]/btn[11]").press
    session.findById("wnd[0]/tbar[0]/btn[3]").press
    session.findById("wnd[0]/tbar[0]/btn[3]").press

    except:
    print(sys.exc_info()[0])
    finally:
    session = None
    connection = None
    application = None
    SapGuiAuto = None

    saplogin()

    Continue reading...

Compartilhe esta Página