如下代码,可以通过python自动登录SAP,处理相关业务后,并自动退出SAP
import win32com.client
import pandas as pd
import sys, time
import win32gui,win32con,time,subprocess
class pyAutoSap():
# 初始化类是可以指定具体需要连接的服务器
def __init__(self,conn_name="S4"):
self.sap_app = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
self.conn_name = conn_name
def user_info(self):
path = r"rapsap\conf.xlsx"
userinfo = pd.read_excel(path, sheet_name="conf", index_col=0).astype(str)
client = userinfo.iloc[0,0]
user_name = userinfo.iloc[0,1]
password = userinfo.iloc[0,2]
language = userinfo.iloc[0,3]
return client, user_name, password, language
def get_sap_session(self):
try:
print('start get_sap_session')
sapgui = win32com.client.GetObject("SAPGUI")
app = sapgui.GetScriptingEngine
conn = app.Children(0)
time.sleep(2)
session = conn.Children(0)
print('successfully get sap session')
return session
except:
print(sys.exc_info()[0])
def get_sapgui(self):
subprocess.Popen(self.sap_app)
time.sleep(5)
#获取GUI右上角界面上的过滤器
flag = 0
while flag == 0:
try:
hwnd = win32gui.FindWindow(None,"SAP Logon 770")
flag = win32gui.FindWindowEx(hwnd,None,"Edit", None)
except:
time.sleep(0.5)
#替换sap gui中连接的名称,
win32gui.SendMessage(flag,win32con.WM_SETTEXT, None, self.conn_name)
win32gui.SendMessage(flag,win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
win32gui.SendMessage(flag,win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
time.sleep(1)
#点击GUI左上角登陆(0)
dlg = win32gui.FindWindowEx(hwnd,None,"Button", None)
win32gui.SendMessage(dlg,win32con.WM_LBUTTONDOWN,0)
win32gui.SendMessage(dlg,win32con.WM_LBUTTONUP,0)
def login(self):
# 用户信息
userinfo = self.user_info()
# print(userinfo)
# 获取具体需登陆的链接
self.get_sapgui()
session = self.get_sap_session()
try:
session.findById("wnd[0]/usr/txtRSYST-MANDT").text = userinfo[0] # 集团
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = userinfo[1] # SAP登陆用户名
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = userinfo[2] # SAP登陆密码
session.findById("wnd[0]/usr/txtRSYST-LANGU").text = userinfo[3] # SAP登陆语言
session.findById("wnd[0]").sendVKey(0)
except:
print(sys.exc_info()[0])
print("Login successfully")
def logout(self):
print("Start Login out")
session = self.get_sap_session()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nex" # 输入T-code
session.findById("wnd[0]").sendVKey(0) # 回车
print("Logout Succesfully")
class bizAuto():
def __init__(self, name=""):
self.name = name
def zmm02(self):
session = pyAutoSap().get_sap_session()
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmm02"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "2487"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW").getAbsoluteRow(0).selected = True
session.findById("wnd[1]").sendVKey(0)
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP01/ssubTABFRA1:SAPLMGMM:2004/subSUB1:SAPLMGD1:1002/txtMAKT-MAKTX").text = "test22221"
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP01/ssubTABFRA1:SAPLMGMM:2004/subSUB1:SAPLMGD1:1002/txtMAKT-MAKTX").caretPosition = 9
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press()
message_type = session.findById("wnd[0]/sbar").MessageType
print(message_type)
text = session.findById("wnd[0]/sbar").Text
# t1 = text.encode("utf-8")
print(text)
with open('a.txt','a+',encoding='utf8') as f:
f.write("消息类型: " + message_type + "/n")
f.write("返回消息:" + text + "/n")
if __name__ == "__main__":
mysap = pyAutoSap()
mysap.login()
bizAuto().zmm02()
time.sleep(10)
mysap.logout()