Python 类别';pywintypes.com_错误';用于运行SAP事务

Python 类别';pywintypes.com_错误';用于运行SAP事务,python,sap-gui,Python,Sap Gui,我正在尝试使用Python脚本(Spyder)打开SAP并运行事务。为了实现这一目标,我录制了SAP vbscript,并对Python的使用进行了一些调整,直到它进入SAP脚本的中间并触发类'pywintypes.com_error'! 我用于执行此任务的代码如下。非常感谢您对如何解决此问题的任何帮助: def saplogin(): try: path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.

我正在尝试使用Python脚本(Spyder)打开SAP并运行事务。为了实现这一目标,我录制了SAP vbscript,并对Python的使用进行了一些调整,直到它进入SAP脚本的中间并触发
类'pywintypes.com_error'
! 我用于执行此任务的代码如下。非常感谢您对如何解决此问题的任何帮助:

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

我使用了相同的代码,但在这一部分:

"except:
        print(sys.exc_info()[0])"
您需要包括其他行:
“print(sys.exc_info())”

请删除
[0]
,然后重试。可能会显示如下尺寸:

(,com_错误(-2147352567,'Exceção.,(619,'SAP前端服务器','无法通过id找到控件。'


在此之后,您将获得更多有关解除Bug的信息。

不确定您是否尝试了此操作,但看起来您的连接名只有“SAP”。请查看SAP登录板,并在那里获取完整的连接名。

适用于Phyton 32和64位版本3.9和2.7

您需要考虑三件事:

  • SAP Gui脚本在SAP应用程序服务器端默认情况下处于禁用状态,导致此错误,因此您需要在SAP Gui事务RZ11中首先通过设置概要文件参数
    sapgui/user_scripting=TRUE

  • 在SAP登录SAP Gui选项中,当脚本连接到SAP Gui时,
    通知未标记,
    当脚本打开连接时通知未标记,
    通知

  • 需要关闭电脑上的SAP登录和SAP Gui


  • 另外,请查看Stefan的Schnell博客文章,并附上评论。

    这不是VBScript。谢谢,您能提供更多详细信息吗?!我不确定到底是什么问题?如果有此领域经验的人能够发表评论,以避免未回答,我将不胜感激。