我正在尝试使用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()
不确定您是否尝试了此操作,但看起来您的连接名只是“SAP”。查看您的SAP登录板,并在那里获取完整的连接名称
我使用了相同的代码,但在这一部分:
您需要包括其他行:
"print(sys.exc_info())"
请删除
[0]
并重试。可能会显示如下尺寸:在这之后,你有更多的信息去除虫
适用于Phyton 32和64位版本3.9和2.7
您需要考虑三件事:
SAP Gui脚本在默认情况下在SAP应用程序服务器端被禁用,这正是导致此错误的原因,因此您需要在SAP Gui事务RZ11中首先通过设置概要文件参数
sapgui/user_scripting=TRUE
来启用它在SAP登录SAP Gui选项中}标记,{}未标记,{}未标记
Accessibility & Scripting
->Scripting User Settings
{需要关闭电脑上的SAP登录和SAP Gui
另外,请查看Stefan的Schnell博客帖子"How to use SAP GUI Scripting inside Python Programming Language",并附上评论
相关问题 更多 >
编程相关推荐