class excelApp():
def closeExcel(slef):
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False
# 可以设置输入提示,和用户交互
# _ = input("Press enter to close Excel")
excel.Application.Quit()
def csv2xlsx(path,csv_name):
print(path + f"/{csv_name}" + ".csv")
csv = pd.read_csv(path + f"/{csv_name}" + ".csv",encoding='utf8')
print(csv)
csv.to_excel( csv_name + ".xlsx",sheet_name=csv_name,index=None,header=["ponumber"])
def xlsx2csv(path,xlsx_name,csv_name):
data_xlsx = pd.read_excel(path + f"{xlsx_name}" +'.xlsx', index_col=None)
data_xlsx.to_csv(path + "/" + csv_name +'.csv', encoding='utf-8',index=None)
采购信息记录示例
def zzme11(self):
session = pyAutoSap().get_sap_session()
session.findById("wnd[0]").maximize()
path1 = sapConfig().getpath()
print(path1)
podata = pd.read_excel(path1 + "zme11.xlsx", sheet_name="sheet1",dtype=str).astype(str)
for i, item in podata.iterrows():
session.findById("wnd[0]/tbar[0]/okcd").text = "/nme11"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtEINA-LIFNR").text = item.lifnr
session.findById("wnd[0]/usr/ctxtEINA-MATNR").text = item.material
session.findById("wnd[0]/usr/ctxtEINE-EKORG").text = "2400"
session.findById("wnd[0]/usr/ctxtEINE-WERKS").text = item.plant
session.findById("wnd[0]/usr/ctxtEINE-WERKS").setFocus()
session.findById("wnd[0]/usr/ctxtEINE-WERKS").caretPosition = 4
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/txtEINE-APLFZ").text = "3"
session.findById("wnd[0]/usr/txtEINE-NORBM").text = "1"
session.findById("wnd[0]/usr/ctxtEINE-MWSKZ").text = "J1"
session.findById("wnd[0]/usr/txtEINE-NETPR").text = item.price
session.findById("wnd[0]/usr/txtEINE-NETPR").setFocus()
session.findById("wnd[0]/usr/txtEINE-NETPR").caretPosition = 14
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/tbar[0]/btn[11]").press()
批量创建用户
def zsu01(self):
session = pyAutoSap().get_sap_session()
session.findById("wnd[0]").maximize()
path1 = sapConfig().getpath()
podata = pd.read_excel(path1 + "userlist.xlsx", sheet_name="Sheet1",skiprows=1).astype(str)
for i, item in podata.iterrows():
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nsu01"
session.findById("wnd[0]").sendVKey(0)
# print(item.data2.zfill(5))
session.findById("wnd[0]/usr/ctxtSUID_ST_BNAME-BNAME").text = item.data2.zfill(8)
session.findById("wnd[0]/usr/ctxtSUID_ST_BNAME-BNAME").caretPosition = 8
session.findById("wnd[0]/tbar[1]/btn[8]").press()
if item.data4 == '男':
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpADDR/ssubMAINAREA:SAPLSUID_MAINTENANCE:1900/cmbSUID_ST_NODE_PERSON_NAME_EXT-TITLE_MEDI").key = "0002"
if item.data4 == '女':
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpADDR/ssubMAINAREA:SAPLSUID_MAINTENANCE:1900/cmbSUID_ST_NODE_PERSON_NAME_EXT-TITLE_MEDI").key = "0001"
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpADDR/ssubMAINAREA:SAPLSUID_MAINTENANCE:1900/txtSUID_ST_NODE_PERSON_NAME-NAME_LAST").text = item.data1
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpADDR/ssubMAINAREA:SAPLSUID_MAINTENANCE:1900/txtSUID_ST_NODE_WORKPLACE-DEPARTMENT").text = item.data5
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpADDR/ssubMAINAREA:SAPLSUID_MAINTENANCE:1900/txtSUID_ST_NODE_COMM_DATA-MOB_NUMBER").text = item.data3
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpADDR/ssubMAINAREA:SAPLSUID_MAINTENANCE:1900/txtSUID_ST_NODE_COMM_DATA-MOB_NUMBER").setFocus()
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpADDR/ssubMAINAREA:SAPLSUID_MAINTENANCE:1900/txtSUID_ST_NODE_COMM_DATA-MOB_NUMBER").caretPosition = 11
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO").select()
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD").text = item.data6
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD2").text = item.data6
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD2").setFocus()
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpLOGO/ssubMAINAREA:SAPLSUID_MAINTENANCE:1101/pwdSUID_ST_NODE_PASSWORD_EXT-PASSWORD2").caretPosition = 12
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpDEFA").select()
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpDEFA/ssubMAINAREA:SAPLSUID_MAINTENANCE:1105/cmbSUID_ST_NODE_DEFAULTS-DCPFM").key = "X"
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpDEFA/ssubMAINAREA:SAPLSUID_MAINTENANCE:1105/cmbSUID_ST_NODE_DEFAULTS-DATFM").key = "4"
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpDEFA/ssubMAINAREA:SAPLSUID_MAINTENANCE:1105/cmbSUID_ST_NODE_DEFAULTS-DATFM").setFocus()
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpPROF").select()
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpPROF/ssubMAINAREA:SAPLSUID_MAINTENANCE:1103/cntlG_PROFILES_CONTAINER/shellcont/shell").contextMenu()
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpPROF/ssubMAINAREA:SAPLSUID_MAINTENANCE:1103/cntlG_PROFILES_CONTAINER/shellcont/shell").modifyCell(0, "PROFILE", "SAP_ALL")
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpPROF/ssubMAINAREA:SAPLSUID_MAINTENANCE:1103/cntlG_PROFILES_CONTAINER/shellcont/shell").modifyCell(1, "PROFILE", "SAP_NEW")
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpPROF/ssubMAINAREA:SAPLSUID_MAINTENANCE:1103/cntlG_PROFILES_CONTAINER/shellcont/shell").currentCellRow = 1
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpPROF/ssubMAINAREA:SAPLSUID_MAINTENANCE:1103/cntlG_PROFILES_CONTAINER/shellcont/shell").pressEnter()
session.findById("wnd[0]/tbar[0]/btn[11]").press()
time.sleep(1)
批量创建BAT快捷登录方式。
Excel模板
def logon_shotcut(self):
"""批量自动生成SAP快就登陆方式的脚本"""
tab = '\t'
print(self.path)
data = pd.read_excel(self.path+'sap_config.xlsx', sheet_name="Sheet1",skiprows=1).astype(str) # sheet1
with open('saplogon.bat','w+',encoding='utf-8') as f:
f.write('@echo off'+'\n')
f.write("chcp 65001>nul"+'\n')
f.write('title SAP快捷登录'+'\n')
f.write( 'echo ' + '序号'+ tab +
'系统' + tab +
'集团' + tab +
'用户名' + tab +
'语言'+ tab +
'系统描述' + tab +
'备注'+'\n')
for i, item in data.iterrows():
f.write('echo ' + str(i) + tab +
item.sys_sign + tab +
item.client + tab +
item.username + tab +
item.lang + tab +
item.description + tab +
item.remarks + tab +
'\n')
f.write(":main"+'\n')
f.write("set /p opt=请选择登录系统的序号:"+'\n')
f.write("call:do%opt%"+'\n')
f.write("if %errorlevel%==1 goto error"+'\n')
for i, item in data.iterrows():
f.write(':do'+ str(i) + '\n'+'start sapshcut.exe -maxgui '+
'-system=' + item.sys_sign + ' ' +
'-client=' + item.client + ' ' +
'-user=' + item.username + ' ' +
'-pw=' + item.passwd + ' ' +
'-l='+ item.lang + '\n' + 'goto main ' + '\n')
f.write(":error"+'\n')
f.write("echo 输入错误"+'\n')
f.write("goto main"+'\n')
清空事务代码初始屏幕自动带出的字段
# 如果用户已经登陆时,获取相关的session
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)
session.findById("wnd[0]").maximize
print('successfully get sap session')
time.sleep(2)
return session
except:
print(sys.exc_info()[0])
def sessionElementInitialization(self):
"""清空SAP GUI 屏幕默认带出的字段"""
session = self.get_sap_session()
usedArea = session.findById("wnd[0]/usr")
self.clear_Fields(usedArea)
def clear_Fields(self,area):
""" 查找屏幕的元素,并通过递归方式清空屏幕默认字段"""
sapgui = win32com.client.GetObject("SAPGUI")
app = sapgui.GetScriptingEngine
counts = area.Children.Count - 1
for i in range(counts):
obj = area.Children(i)
# 如果列表为空,则无法找到
regxList = re.findall('TextField',obj.Type)
if obj.ContainerType == True:
if obj.Children.Count > 0:
nextArea = app.FindById(obj.Id)
clear_Fields(nextArea)
if len(regxList) > 0 and obj.changeable == True:
obj.text = ""
elif obj.Type == "GuiCheckBox":
obj.selected = False