【问题标题】:class 'pywintypes.com_error' for running a SAP transaction用于运行 SAP 事务的类 'pywintypes.com_error'
【发布时间】:2020-01-06 21:23:12
【问题描述】:

我正在尝试使用 Python (Spyder) 中的脚本打开 SAP 并运行事务。为了实现这个目标,我记录了 SAP vbscript,并通过对 Python 使用的一些调整,它工作正常,直到它到达 SAP 脚本的中间并触发class '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()

【问题讨论】:

  • 那不是VBScript。
  • 谢谢,你能提供更多细节吗?!我不确定到底出了什么问题?
  • 如果有该领域经验的人发表评论以避免无人回答,我将不胜感激。

标签: python sap-gui


【解决方案1】:

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

"except:
        print(sys.exc_info()[0])"

您需要包含其他行:"print(sys.exc_info())"

删除[0] 并重试。可能会显示如下消息:

"(, com_error(-2147352567, 'Exceção.', (619, 'SAP Frontend Server', '无法通过id找到控件。' "

除此之外,您还有更多的调试信息。

【讨论】:

    【解决方案2】:

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

    您需要考虑的三件事:

    1. SAP 应用服务器端默认禁用 SAP Gui 脚本,导致出现此错误,因此您的 Basis 需要先在 SAP Gui 事务 RZ11 中通过设置配置文件参数sapgui/user_scripting=TRUE 启用它

    2. 在 SAP Logon SAP Gui 选项中 Accessibility & Scripting -> Scripting User Settings Enable scripting 标记,Notify when a script attaches to SAP GUI 未标记,Notify when a script opens a connection 未标记。

    3. 您的 PC 上的 SAP Logon 和 SAP Gui 需要关闭

    另外,请查看 Stefan 的 Schnell 博客文章 "How to use SAP GUI Scripting inside Python Programming Language" 与 cmets。

    【讨论】:

      【解决方案3】:

      不确定您是否尝试过,但看起来您只是将“SAP”作为连接名称。查看您的 SAP Logon Pad 并在此处获取完整的连接名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-09
        • 1970-01-01
        • 2012-09-03
        • 2013-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多