我有一个代码尝试打开 SAP GUI 应用程序,在输入用户凭据后,它运行一个事务并在本地下载
.xlsx
文件。
代码的第二部分是我在 SAP GUI 中录制并粘贴到 Spyder 中的确切
.vbs
脚本。
我在
行遇到语法错误,如何解决?if not IsObject(application) Then
注意:经过多次编辑后,我尝试使用与 Spyder 中使用的相同的缩进。
< Importing the Libraries >
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 = "SP032.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()
I did the changes after Sandra Rossi's comment as following but get a new error:
导入win32com.client 导入系统 导入子流程 导入时间
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("=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)
session.findById("wnd[0]").resizeWorkingPane (98,16,False)
session.findById("wnd[0]/tbar[0]/okcd").text = "SP032"
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]/usr").verticalScrollbar.position = 1
session.findById("wnd[0]/usr").verticalScrollbar.position = 2
session.findById("wnd[0]/usr").verticalScrollbar.position = 3
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:\86A"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "SP032.txt"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 9
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
saplogin()
注意:正如我在评论中提到的,我做了很多修改,但在最后一个修改之后,奇怪的错误仍然返回为**IndentationError:意外的未缩进
一些简单的代码(例如方法调用或属性初始化)在 VBScript 和 Python 中具有相同的语法,但其余部分不兼容。
例如,您必须进行这些调整(摘自 Stefan Schnell 博客文章):
Python(等效的 VBScript 代码已被注释掉):
#session.findById("wnd[0]").resizeWorkingPane 173, 36, 0
session.findById("wnd[0]").resizeWorkingPane(173, 36, 0)
#session.findById("wnd[0]/tbar[0]/okcd").text = "/nse16"
session.findById("wnd[0]/tbar[0]/okcd").text = "/nse16"
#session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey(0)
#session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
#session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey(0)
#session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press()
现在看来,没有有效 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
关于缩进问题,StackOverflow 中有很多问题和解答。