关于通用技能类别

(将第一段替换为新类别的简要说明。此指导将显示在类别选择区域中,因此请尽量将其保持在 200 个字符以内。)

使用以下段落提供详细描述或者建立类别准则或规则:

  • 为什么应使用此类别?它用来做什么?

  • 此类别和我们已经有的类别究竟有什么不同?

  • 此类别中的话题一般包含什么?

  • 我们需要此类别吗?我们可以将其与其他类别或子类别合并吗?

如下代码,可以通过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()