SAP前台批量创建采购订单

第一版


    def zme21n(self):
        # path1 = sapConfig().getpath()
        session = pyAutoSap().get_sap_session()

        podata = pd.read_excel(r"C:\podata.xlsx", sheet_name="po").astype(str) 
        data = pd.read_excel(r"C:\podata.xlsx", sheet_name="data").astype(str) 
        
        # 循环订单的个数
        for i, rowpo in podata.iterrows():
            
            # k用于处理下一个po,行项目归零
            k = 0
            session.findById("wnd[0]").maximize()
            session.findById("wnd[0]/tbar[0]/okcd").text = "/NME21N"
            session.findById("wnd[0]").sendVKey(0)

            # 抬头信息
            session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/ctxtMEPO_TOPLINE-SUPERFIELD").text = rowpo.vendor
            session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT8/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-EKORG").text = rowpo.po_org
            session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT8/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-EKGRP").text = rowpo.po_grp
            session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT8/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-BUKRS").text = "H100"
            session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT8/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-BUKRS").setFocus()
            session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT8/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221/ctxtMEPO1222-BUKRS").caretPosition = 4
            session.findById("wnd[0]").sendVKey(0)

            # 循环订单详细信息   
            for j, row in data.iterrows():

                if rowpo.po_number == row.po_number:

                    session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EMATN[6,"+ str(k) +"]").text = row.material
                    session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-TXZ01[7,"+ str(k) +"]").text = ""
                    session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-MENGE[8,"+ str(k) +"]").text = row.quantity
                    session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-NAME1[18,"+ str(k) +"]").text = row.plant
                    session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-NAME1[18,0]").setFocus()
                    session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-NAME1[18,0]").caretPosition = 4
                    
                    time.sleep(2)

                    k = k + 1   

            # 保存退出
            session.findById("wnd[0]").sendVKey(0)
            session.findById("wnd[0]/tbar[0]/btn[11]").press()
            session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press()

抬头信息,对应po工作表

po_number po_org po_grp po_com vendor
10000 H100 H01 H100 1000139
10001 H100 H01 H100 1000139

行项目信息,对应data工作表,数据如下表:

注意,此时我们已经维护物料的单价,故再创建采购订单时会自动点出

po_number material quantity plant
10000 2596 11 H102
10000 2596 22 H102
10000 2596 33 H102
10001 2596 1 H102
10001 2596 2 H102
10001 2596 3 H102
10001 2596 4 H102
10001 2596 5 H102
10001 2596 6 H102

第二版

第二版注重于代码的通用性和,代码的优化


    def zme21n_new(self):

        """
        批量自动创建采购订单
        """
        path1 = sapConfig().getpath()
        session = pyAutoSap().get_sap_session()

        podata = pd.read_excel(path1 + "podata.xlsx", sheet_name="po").astype(str)
        data = pd.read_excel(path1 + "podata.xlsx", sheet_name="data").astype(str)
        
        # 抬头部分共用字段
        commonstr1 = "wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB1:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1102/tabsHEADER_DETAIL/tabpTABHDT8/ssubTABSTRIPCONTROL2SUB:SAPLMEGUI:1221"
        
        # 行项目部分共用字段
        commonstr2 = "subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211"
        
        # 循环订单的个数
        for i, rowpo in podata.iterrows():
            
            # k用于处理下一个po,行项目归零
            k = 0
            session.findById("wnd[0]").maximize()
            session.findById("wnd[0]/tbar[0]/okcd").text = "/NME21N"
            session.findById("wnd[0]").sendVKey(0)

            # 抬头信息
            session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/ctxtMEPO_TOPLINE-SUPERFIELD").text = rowpo.vendor
            session.findById(commonstr1 + "/ctxtMEPO1222-EKORG").text = rowpo.po_org
            session.findById(commonstr1 + "/ctxtMEPO1222-EKGRP").text = rowpo.po_grp
            session.findById(commonstr1 + "/ctxtMEPO1222-BUKRS").text = "H100"
            session.findById(commonstr1 + "/ctxtMEPO1222-BUKRS").setFocus()
            session.findById(commonstr1 + "/ctxtMEPO1222-BUKRS").caretPosition = 4
            session.findById("wnd[0]").sendVKey(0)

            # 循环订单详细信息   
            for j, row in data.iterrows():

                if rowpo.po_number == row.po_number:
                    
                    if k == 0:
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/" + commonstr2 + "/ctxtMEPO1211-EMATN[6,0]").text = row.material
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/" + commonstr2 + "/txtMEPO1211-MENGE[8,0]").text = row.quantity
                        # session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/" + commonstr2 + "/txtMEPO1211-NETPR[13,0]").text = "100" # 单价
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/" + commonstr2 + "/ctxtMEPO1211-NAME1[18,0]").text = row.plant
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/" + commonstr2 + "/ctxtMEPO1211-NAME1[18,0]").setFocus()
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0013/" + commonstr2 + "/ctxtMEPO1211-NAME1[18,0]").caretPosition = 4
                        session.findById("wnd[0]").sendVKey(0)
                        k = 1
                        time.sleep(1)
                    else:
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/" + commonstr2 + "").verticalScrollbar.position = k
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/" + commonstr2 + "/ctxtMEPO1211-EMATN[6,1]").text = row.material
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/" + commonstr2 + "/txtMEPO1211-MENGE[8,1]").text = row.quantity
                        # session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/" + commonstr2 + "/txtMEPO1211-NETPR[13,1]").text = "100" #单价
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/" + commonstr2 + "/ctxtMEPO1211-NAME1[18,1]").text = row.plant
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/" + commonstr2 + "/ctxtMEPO1211-NAME1[18,1]").setFocus()
                        session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0010/" + commonstr2 + "/ctxtMEPO1211-NAME1[18,1]").caretPosition = 4
                        session.findById("wnd[0]").sendVKey(0)
                        k = k + 1 


            # 保存
            session.findById("wnd[0]").sendVKey(0)
            session.findById("wnd[0]/tbar[0]/btn[11]").press()
            session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press()

            # 获取状态栏的消息
            text = session.findById("wnd[0]/sbar").Text
            
            # 清洗状态栏获取到的数据,只保留订单号
            newtext = text.split(" ")[1]
        
            test = {"po_number" : newtext}
            # 将数据写入csv文件
            new_po = pd.DataFrame(test,index={"1"})
            new_po.to_csv("采购订单.csv",mode='a',encoding='utf-8',header=False,index=False)