作为SAP MM的一名官员,我想对一个应用程序进行编码以完成一些重复的工作。但是,当我记录SAP Gui脚本时,无法在Python中成功运行它。我是否使用了错误的模块或代码有问题?
import win32com.client
import sys
def SAP_OP():
excelPath = r'#'
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.Children(0)
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]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nMM03"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtS_VKORG-LOW").text = "#"
session.findById("wnd[0]/usr/ctxtS_VTWEG-LOW").text = "#"
session.findById("wnd[0]/usr/ctxtP_EKORG").text = "#"
session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").text = "#"
session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").setFocus
session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").caretPosition = 4
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/usr/cntlMAT_CONT/shellcont/shell").currentCellColumn = r"#"
session.findById("wnd[0]/usr/cntlMAT_CONT/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlMAT_CONT/shellcont/shell").selectContextMenuItem = r"&XXL"
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session = None
connection = None
application = None
SapGuiAuto = None
SAP_OP()
答案 0 :(得分:1)
请尝试以下操作。根据您的凭据更改ConnectionName,Username和Password。
# 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("ConnectionName", 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 = "USERNAME"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD"
session.findById("wnd[0]").sendVKey(0)
except:
print(sys.exc_info()[0])
finally:
session = None
connection = None
application = None
SapGuiAuto = None
saplogin()