在python Spyder 3.3中使用SAP Net Weaver GUI vbs脚本

时间:2019-12-27 18:38:29

标签: python vbscript sap

很抱歉,如果我的问题写得不好,因为我在这里很新手。我有一个如下代码,尝试打开SAP Net Weaver GUI应用程序,并在放置用户凭据后,它运行事务并在本地下载.xlsx文件。代码第二部分出现错误,这是我在SAP中记录并粘贴到Spyder中的确切.vbs脚本:

<导入库>

'''

import win32com.client
import sys
import subprocess
import time


##This function will Login to SAP from the SAP Logon window

def saplogin():

    try:

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

        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("=PR1 [Assembly & Test] router 1", True)

        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)
##This part is the SAP .vbs script which is recorded in the SAP and pasted here as-is: **>
        if not IsObject(application) Then ##here I get the invalid syntax error.

            Set SapGuiAuto  = GetObject("SAPGUI")
            Set application = SapGuiAuto.GetScriptingEngine
        End if
        if not IsObject(connection) Then
           Set connection = application.Children(0)
        End if
        if not IsObject(session) Then
           Set session    = connection.Children(0)
        End if
        if IsObject(WScript) Then
           WScript.ConnectObject session,     "on"
           WScript.ConnectObject application, "on"
        End if 
        session.findById("wnd[0]").maximize
        session.findById("wnd[0]/tbar[0]/okcd").text = "SPT52"
        session.findById("wnd[0]/tbar[0]/btn[0]").press

        session.findById("wnd[0]").maximize
        session.findById("wnd[0]/usr/chkP_17REQS").selected = false
        session.findById("wnd[0]/usr/chkP_18REQS").selected = false
        session.findById("wnd[0]/usr/chkP_10REQS").selected = false
        session.findById("wnd[0]/usr/chkP_ENGINE").selected = false
        session.findById("wnd[0]/usr/chkP_DETAIL").selected = false
        session.findById("wnd[0]/usr/radRB_FILE").select
        session.findById("wnd[0]/usr/ctxtP_PLANT").text = "0010"
        session.findById("wnd[0]/usr/ctxtPD_SEL-LOW").text = "86A"
        session.findById("wnd[0]/usr/txtP_FDAY2").text = "0"
        session.findById("wnd[0]/usr/txtP_WEEK2").text = "52"
        session.findById("wnd[0]/usr/txtP_MONTH2").text = "0"
        session.findById("wnd[0]/usr/txtP_FDAY2").setFocus
        session.findById("wnd[0]/usr/txtP_FDAY2").caretPosition = 3
        session.findById("wnd[0]/tbar[1]/btn[8]").press
        session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus
        session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 0
        session.findById("wnd[1]").sendVKey 4
        session.findById("wnd[2]").close
        session.findById("wnd[1]/usr/ctxtDY_PATH").text = "C:\Users\Documents\SAP\SAP GUI\"
        session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "SPT52.xls"
        session.findById("wnd[1]/usr/ctxtDY_FILENAME").setFocus
        session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 8
        session.findById("wnd[1]/tbar[0]/btn[11]").press
        session.findById("wnd[1]/tbar[0]/btn[0]").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()

'''

  

经过几次编辑,我尝试使用与Spyder中相同的缩进。非常感谢您的帮助。   再次谢谢你

1 个答案:

答案 0 :(得分:0)

一些简单的代码(例如方法调用或属性初始化)在VBScript和Python中具有相同的语法,但其余代码不兼容。

现在似乎不需要使用无效的Python语法的代码部分,因为您已经初始化了变量!这些是以下要从脚本中删除的行:

        if not IsObject(application) Then ##here I get the invalid syntax error.

            Set SapGuiAuto  = GetObject("SAPGUI")
            Set application = SapGuiAuto.GetScriptingEngine
        End if
        if not IsObject(connection) Then
           Set connection = application.Children(0)
        End if
        if not IsObject(session) Then
           Set session    = connection.Children(0)
        End if
        if IsObject(WScript) Then
           WScript.ConnectObject session,     "on"
           WScript.ConnectObject application, "on"
        End if