首页 > 解决方案 > 尝试使用 Pywin32 连接到 SAPGUI 时出错

问题描述

我正在使用此功能连接到 SAPGUI:

def sap_connect(local_sap_box):
    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(local_sap_box,True)
    if not type(connection) == win32com.client.CDispatch:
        application = None
        SapGuiAuto = None
        return
    session = connection.Children(0)
    sap_box = session.findById('wnd[0]/sbar/pane[1]').text
    sap_box = sap_box[0:3]
    sap_sbar = session.findById('wnd[0]/sbar').text
    sap_user = sap_sbar[17:23]
    print('User ' + sap_user + ' logged into ' + sap_box)
    if not type(session) == win32com.client.CDispatch:
        connection = None
        application = None
        SapGuiAuto = None
        return
    return session

从今天开始,我收到一个 'com_error: (-2147221020, 'Invalid syntax', None, None)' 错误。

这是完整的错误消息:

---------------------------------------------------------------------------
com_error                                 Traceback (most recent call last)
<ipython-input-3-1c5ab2335269> in <module>
      9 for order_group in order_groups:
     10     for sap_box, currency_settings in order_groups[order_group].items():
---> 11         sap_session = sap_connect(sap_box)
     12         sap_RPX0(sap_session, currency_settings)
     13         kob1 = sap_KOB1(sap_session, order_group, '07/01/2021', '06/30/2022', 'ID_KOB1_ALL')

<ipython-input-2-9c51187610f0> in sap_connect(local_sap_box)
      1 def sap_connect(local_sap_box):
----> 2     SapGuiAuto = win32com.client.GetObject('SAPGUI')
      3     if not type(SapGuiAuto) == win32com.client.CDispatch:
      4         return
      5     application = SapGuiAuto.GetScriptingEngine

~\Anaconda3\lib\site-packages\win32com\client\__init__.py in GetObject(Pathname, Class, clsctx)
     70     return GetActiveObject(Class, clsctx)
     71   else:
---> 72     return Moniker(Pathname, clsctx)
     73 
     74 def GetActiveObject(Class, clsctx = pythoncom.CLSCTX_ALL):

~\Anaconda3\lib\site-packages\win32com\client\__init__.py in Moniker(Pathname, clsctx)
     85     Python friendly version of GetObject's moniker functionality.
     86   """
---> 87   moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
     88   dispatch = moniker.BindToObject(bindCtx, None, pythoncom.IID_IDispatch)
     89   return __WrapDispatch(dispatch, Pathname, clsctx=clsctx)

com_error: (-2147221020, 'Invalid syntax', None, None)

我正在使用 SSO,SAPGUI Logon Pad 是 760。这里出了什么问题?

标签: pythonjupyter-notebookanacondasap-gui

解决方案


推荐阅读