Python处理业务数据案例

, , ,

MB52数据批量导出程序

    def zmb52(self):
        path1 = sapConfig().getpath()
        session = pyAutoSap().get_sap_session()
        session.findById("wnd[0]").maximize
        session.findById("wnd[0]/tbar[0]/okcd").text = "MB52"
        session.findById("wnd[0]").sendVKey(0)
        session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = "ZITL"
        session.findById("wnd[0]/usr/ctxtLGORT-LOW").text = "001"
        session.findById("wnd[0]/usr/ctxtLGORT-LOW").setFocus()
        session.findById("wnd[0]/usr/ctxtLGORT-LOW").caretPosition = 3
        session.findById("wnd[0]/tbar[1]/btn[8]").press()
        session.findById("wnd[0]/tbar[1]/btn[43]").press()
        session.findById("wnd[1]/tbar[0]/btn[0]").press()
        session.findById("wnd[1]/usr/ctxtDY_PATH").text = path1
        session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus()
        session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 50
        session.findById("wnd[1]/tbar[0]/btn[0]").press()
        session.findById("wnd[0]/tbar[0]/btn[3]").press()
        session.findById("wnd[0]/tbar[0]/btn[3]").press()

批量修改货架生命周期,模拟MSC2N


    def zmsc2n(self):

        data = pd.read_excel(self.path, sheet_name="data").astype(str) # sheet1
        for i, row in data.iterrows():
            session = pyAutoSap().get_sap_session()
            session.findById("wnd[0]").maximize()
            session.findById("wnd[0]/tbar[0]/okcd").text = "/nmsc2n"
            session.findById("wnd[0]").sendVKey(0)
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_HEADER:SAPLCHRG:1501/ctxtDFBATCH-MATNR").text = row.MATNR
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_HEADER:SAPLCHRG:1501/ctxtDFBATCH-CHARG").text = row.CHARG
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_HEADER:SAPLCHRG:1501/ctxtDFBATCH-WERKS").text = row.WERKS
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_HEADER:SAPLCHRG:1501/ctxtDFBATCH-LGORT").text = row.LGORT
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_HEADER:SAPLCHRG:1501/ctxtDFBATCH-LGORT").setFocus()
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_HEADER:SAPLCHRG:1501/ctxtDFBATCH-LGORT").caretPosition = 4
            session.findById("wnd[0]").sendVKey(0)
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_TABSTRIP:SAPLCHRG:2000/tabsTS_BODY/tabpGRHD/ssubSUBSCR_BODY:SAPLCHRG:2100/ctxtDFBATCH-MHD_IO").text = row.MHD_IO
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_TABSTRIP:SAPLCHRG:2000/tabsTS_BODY/tabpGRHD/ssubSUBSCR_BODY:SAPLCHRG:2100/ctxtDFBATCH-MHD_IO").setFocus()
            session.findById("wnd[0]/usr/subSUBSCR_BATCH_MASTER:SAPLCHRG:1111/subSUBSCR_TABSTRIP:SAPLCHRG:2000/tabsTS_BODY/tabpGRHD/ssubSUBSCR_BODY:SAPLCHRG:2100/ctxtDFBATCH-MHD_IO").caretPosition = 10
            session.findById("wnd[0]/tbar[0]/btn[11]").press()
    
模拟MM02修改物料描述,并获取左下角的返回消息。可以在此基础上做适当的修改完成其他字段的修改
    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)
        time.sleep(5)
        session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "R999-001"
        session.findById("wnd[0]").sendVKey(0)
        time.sleep(5)
        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 = "R999-001"
        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")