python使用SAP GUI操作SAP的几个坑
1) 首先必须设定SAP中设置可以使用SAP GUI Script.
详细参见以下文章:
https://blog.csdn.net/weixin_44447687/article/details/106216776
简单来说就是 SAP ==>RZ11==>sapgui/user_scripting==>需要设定为true
需要设定为 启用脚本==>下面的两个警告勾勾去掉(否则会出现不需要的弹窗)
2) 需要安装pywin32
pip install pywin32
参照
https://blog.csdn.net/tq501501/article/details/119425042
3) 录制Script
4) Python基本代码如下
#-Begin-----------------------------------------------------------------
#-Includes--------------------------------------------------------------
import sys, win32com.client
import win32api,win32gui,win32con,win32ui,time,os,subprocess
#-Sub Main--------------------------------------------------------------
def Main():
sap_app = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe" #您的saplogon程序本地完整路径
subprocess.Popen(sap_app)
time.sleep(1)
flt=0
while flt==0:
try:
hwnd = win32gui.FindWindow(None,"SAP Logon 740")
flt=win32gui.FindWindowEx(hwnd,None,"Edit", None) #capture handle of filter
except:
time.sleep(0.5)
win32gui.SendMessage(flt,win32con.WM_SETTEXT,None,"DEV")
win32gui.SendMessage(flt,win32con.WM_KEYDOWN,win32con.VK_RIGHT,0)
win32gui.SendMessage(flt,win32con.WM_KEYUP,win32con.VK_RIGHT,0)
time.sleep(0.1)
dlg = win32gui.FindWindowEx(hwnd,None,"Button", None) #登陆(0)
win32gui.SendMessage(dlg,win32con.WM_LBUTTONDOWN,0)
win32gui.SendMessage(dlg,win32con.WM_LBUTTONUP,0)
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
time.sleep(2)
session = connection.Children(0)
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return
print("OK")
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "XXXXXX" #此次放入您的SAP登陆用户名
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "xxxxxx" #此次放入您的SAP登陆密码
session.findById("wnd[0]").sendVKey(0)
"""下面演示了使用mm03查看物料1000000000000的状态后再退回sap首页的过程"""
session.findById("wnd[0]/tbar[0]/okcd").text = "mm03"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "180100069380"
session.findById("wnd[0]").sendVKey(0)
#session.findById("wnd[0]/tbar[0]/okcd").text = "/n"
#session.findById("wnd[0]").sendVKey(0)
#session.findById("wnd[0]/tbar[0]/btn[3]").press()
#session.findById("wnd[0]/tbar[0]/btn[3]").press()
#>Insert your SAP GUI Scripting code here<
#-Main------------------------------------------------------------------
if __name__ == "__main__":
Main()
#-End-------------------------------------------------------------------
参考资料
https://blog.csdn.net/tq501501/article/details/119425042 安装
https://blog.csdn.net/zhichuan0307/article/details/107205883 设定
https://www.cnblogs.com/new-june/p/NewJune.html python代码
https://blog.csdn.net/weixin_44447687/article/details/106216776 设定
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了