python操作SAP登录时,遇到一个奇怪问题,在其它server运行正常的程序,换了一台server不能通过搜索框,搜索。
经过排查发现有三个相同类名的编辑框,类名为“Edit”,第二个为正确的,加入列表取中间值,搞定。
官方文档
win32gui.GetClassName(hwnd) #获取类名
win32gui.IsWindowEnabled(hwnd) #窗口是否可用,1,0
win32gui.IsWindowVisible(hwnd) #窗口是否可见 1,0
win32gui.GetWindowText(hwnd) #获取窗口标题
edit_list = []
def all_ok(hwnd, param):
text = win32gui.GetClassName(hwnd)
if text == 'Edit':
print(hwnd,text, win32gui.IsWindowEnabled(hwnd), win32gui.IsWindowVisible(hwnd))
edit_list.append(hwnd)
return True
重点部分
flt = 0
while flt == 0:
try:
hwnd = win32gui.FindWindow(None, sap_version)
flt = win32gui.FindWindowEx(hwnd, None, "Edit", None)
print('hwnd', hwnd)
print('flt', flt)
win32gui.EnumChildWindows(hwnd, all_ok, None)
flt = edit_list[1]
print('flt', flt)
time.sleep(3)
print('---------------->')
except:
print('except============>')
time.sleep(0.5)
print('search_name', search_name)
win32gui.SendMessage(flt, win32con.WM_SETTEXT, None, search_name)
win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
time.sleep(3)
# 登录GUI界面
参考代码
import win32gui
hwnd_title = dict()
def get_all_hwnd(hwnd,mouse):
if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)})
win32gui.EnumWindows(get_all_hwnd, 0)
for h,t in hwnd_title.items():
if t is not "":
print(h, t)
https://blog.csdn.net/gamers/article/details/82423128
完整代码
import win32com.client, wmi, os, json, arrow, re
import win32gui, win32con, time, subprocess
import sqlite3, xlrd, os
from config import current_day, current_week_num, sunday, saturday, download_directory
from models import *
from models import session as sql_session
from write_to_excel_isp import isp_to_excel
from datetime import datetime
today = datetime.today().strftime('%Y%m%d')
edit_list = []
def all_ok(hwnd, param):
text = win32gui.GetClassName(hwnd)
if text == 'Edit':
print(hwnd,text, win32gui.IsWindowEnabled(hwnd), win32gui.IsWindowVisible(hwnd))
edit_list.append(hwnd)
return True
# 从excel获取SAP登录信息
def get_params(filename):
data = xlrd.open_workbook(filename)
sheet_name = 'Sheet1'
table = data.sheet_by_name(sheet_name)
# print('sheet1', table.nrows)
path = table.cell(1, 0).value
version = table.cell(1, 1).value
search_name = table.cell(1, 2).value
username = table.cell(1, 3).value
password = table.cell(1, 4).value
username2 = table.cell(1, 5).value
password2 = table.cell(1, 6).value
query_date = table.cell(1, 7).value
wait_time = table.cell(1, 8).value
# print('path', path)
# print('version', version)
# print('search_name', search_name)
# print('username', username)
# print('password', password)
# print('username2', username2)
# print('password2', password2)
# print('query_date', query_date)
# print('wait_time', wait_time)
# return path, version, search_name, username, password, username2, password2, wait_time
return path, version, search_name, username, password, username2, password2, query_date, wait_time
# 杀掉sap进程
def kill_sap():
c = wmi.WMI()
for process in c.Win32_Process(name="saplogon.exe"):
print(process.ProcessId, process.Name)
process.Terminate()
for process in c.Win32_Process(name="guixt.exe"):
print(process.ProcessId, process.Name)
process.Terminate()
# sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params()
def sap_login(sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time, client='807'):
'''
登录SAP, 输入T-code, 返回获取的session
:return: session
'''
kill_sap()
sap_app = sap_path # 您的saplogon程序本地完整路径
subprocess.Popen(sap_app)
time.sleep(int(wait_time))
flt = 0
while flt == 0:
try:
hwnd = win32gui.FindWindow(None, sap_version)
flt = win32gui.FindWindowEx(hwnd, None, "Edit", None)
print('hwnd', hwnd)
print('flt', flt)
win32gui.EnumChildWindows(hwnd, all_ok, None)
flt = edit_list[1]
print('flt', flt)
time.sleep(3)
print('---------------->')
except:
print('except============>')
time.sleep(0.5)
print('search_name', search_name)
win32gui.SendMessage(flt, win32con.WM_SETTEXT, None, search_name)
win32gui.SendMessage(flt, win32con.WM_KEYDOWN, win32con.VK_RIGHT, 0)
win32gui.SendMessage(flt, win32con.WM_KEYUP, win32con.VK_RIGHT, 0)
time.sleep(3)
# 登录GUI界面
time.sleep(0.3)
dlg = win32gui.FindWindowEx(hwnd, None, "Button", None)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)
time.sleep(2)
SapGuiAuto = win32com.client.GetObject("SAPGUI")
print(SapGuiAuto)
print(type(SapGuiAuto))
if not type(SapGuiAuto) == win32com.client.CDispatch:
return
application = SapGuiAuto.GetScriptingEngine
print('application', application)
print('count', application.Children.Count)
while not application.Children.Count:
time.sleep(0.5)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONDOWN, 0)
win32gui.SendMessage(dlg, win32con.WM_LBUTTONUP, 0)
print('count 0 sleep 0.5')
SapGuiAuto = win32com.client.GetObject("SAPGUI")
application = SapGuiAuto.GetScriptingEngine
print('sleep 3')
print('hello')
if not type(application) == win32com.client.CDispatch:
SapGuiAuto = None
print('return')
return
connection = application.Children(0)
if not type(connection) == win32com.client.CDispatch:
application = None
SapGuiAuto = None
print('return2')
return
time.sleep(2)
# session = connection.Children(0)
flag = 0
while flag == 0:
try:
connection = application.Children(0)
session = connection.Children(0)
print(session)
flag = 1
except:
print('time sleep 0.5',application.Children.Count)
time.sleep(0.5)
# print('type session', type(session))
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return
print('client--->', client)
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/usr/txtRSYST-MANDT").text = client
session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "EN"
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = username
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = password
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus()
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
session.findById("wnd[0]").sendVKey(0)
# 出现多用户登录
# License Information for Multiple Logon
multi_logon_text = session.findById("wnd[1]").text
if 'License Information' in multi_logon_text:
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").select()
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").setFocus()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
# 出现版权点击确定,没有版权提示直接进行下一步
try:
x = session.findById("wnd[1]").text
# print(x)
if 'Copyright' in x:
session.findById("wnd[1]/tbar[0]/btn[0]").press()
except:
pass
# 出现二级密码登录,不出现跳过
try:
session.findById("wnd[1]/usr/txtGS_OUT-ID").text = username2
session.findById("wnd[1]/usr/pwdGS_OUT-PW").text = password2
session.findById("wnd[1]/usr/pwdGS_OUT-PW").setFocus()
session.findById("wnd[1]/usr/pwdGS_OUT-PW").caretPosition = 10
session.findById("wnd[1]/usr/btnLOGIN").press()
except:
print('no second user')
return session
def get_ubj9(session,current_day, sunday, saturday, name):
filename = download_directory + name + "_week_" + current_day +".xls"
if not os.path.exists(filename):
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb51"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = name
session.findById("wnd[0]/usr/ctxtBWART-LOW").text = "Z01"
session.findById("wnd[0]/usr/ctxtBWART-LOW").setFocus()
session.findById("wnd[0]/usr/ctxtBWART-LOW").caretPosition = 3
session.findById("wnd[0]/usr/btn%_BWART_%_APP_%-VALU_PUSH").press()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,1]").text = "Z02"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,2]").text = "601"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").text = "602"
# session.findById(
# "wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,4]").text = "Z61"
# session.findById(
# "wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,5]").text = "Z62"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").setFocus()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").caretPosition = 3
session.findById("wnd[1]/tbar[0]/btn[8]").press()
session.findById("wnd[0]/usr/ctxtBUDAT-LOW").text = sunday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").text = saturday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").setFocus()
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/tbar[1]/btn[48]").press()
session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[2]").select()
session.findById("wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").select()
session.findById("wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").setFocus()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").text = download_directory + name + "_week_" + current_day +".xls"
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").caretPosition = 45
session.findById("wnd[1]").sendVKey(0)
else:
print('have this file')
def get_ubj9_isp(session,current_day, sunday, saturday, name):
filename = download_directory + name + "_isp_week_" + current_day +".xls"
if not os.path.exists(filename):
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb51"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = name
session.findById("wnd[0]/usr/ctxtBWART-LOW").text = "Z01"
session.findById("wnd[0]/usr/ctxtBWART-LOW").setFocus()
session.findById("wnd[0]/usr/ctxtBWART-LOW").caretPosition = 3
session.findById("wnd[0]/usr/btn%_BWART_%_APP_%-VALU_PUSH").press()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,1]").text = "Z02"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,2]").text = "601"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").text = "602"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,4]").text = "Z61"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,5]").text = "Z62"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").setFocus()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").caretPosition = 3
session.findById("wnd[1]/tbar[0]/btn[8]").press()
session.findById("wnd[0]/usr/ctxtBUDAT-LOW").text = sunday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").text = saturday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").setFocus()
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/tbar[1]/btn[48]").press()
session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[2]").select()
session.findById("wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").select()
session.findById("wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").setFocus()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").text = download_directory + name + "_isp_week_" + current_day +".xls"
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").caretPosition = 45
session.findById("wnd[1]").sendVKey(0)
else:
print('have this file')
def get_hbj9(session,current_day, sunday, saturday, name):
filename = download_directory + name + "_week_" + current_day +".xls"
if not os.path.exists(filename):
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb51"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = name
session.findById("wnd[0]/usr/ctxtBWART-LOW").text = "Z01"
session.findById("wnd[0]/usr/ctxtBWART-LOW").setFocus()
session.findById("wnd[0]/usr/ctxtBWART-LOW").caretPosition = 3
session.findById("wnd[0]/usr/btn%_BWART_%_APP_%-VALU_PUSH").press()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,1]").text = "Z02"
# session.findById(
# "wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,1]").text = "11"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,2]").text = "601"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").text = "602"
# session.findById(
# "wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,4]").text = "Z61"
# session.findById(
# "wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,5]").text = "Z62"
# time.sleep(1000)
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").setFocus()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").caretPosition = 3
session.findById("wnd[1]/tbar[0]/btn[8]").press()
session.findById("wnd[0]/usr/ctxtBUDAT-LOW").text = sunday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").text = saturday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").setFocus()
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/tbar[1]/btn[48]").press()
session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[2]").select()
#20220105
# session.findById(
# "wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select()
session.findById("wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").select()
# session.findById(
# "wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").text = download_directory + name + "_week_" + current_day +".xls"
# session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = download_directory + name + "_week_" + current_day +".xls"
# session.findById("wnd[1]/usr/ctxtDY_PATH").text = download_directory
# session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = name + "_week_" + current_day +".xls"
session.findById("wnd[1]/tbar[0]/btn[0]").press()
# session.findById("wnd[1]/tbar[0]/btn[0]").press()
# session.findById("wnd[1]").sendVKey(0)
else:
print('have this file')
def get_hbj9_isp(session,current_day, sunday, saturday, name):
filename = download_directory + name + "_isp_week_" + current_day +".xls"
if not os.path.exists(filename):
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb51"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = name
session.findById("wnd[0]/usr/ctxtBWART-LOW").text = "Z01"
session.findById("wnd[0]/usr/ctxtBWART-LOW").setFocus()
session.findById("wnd[0]/usr/ctxtBWART-LOW").caretPosition = 3
session.findById("wnd[0]/usr/btn%_BWART_%_APP_%-VALU_PUSH").press()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,1]").text = "Z02"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,2]").text = "601"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").text = "602"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,4]").text = "Z61"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,5]").text = "Z62"
# time.sleep(1000)
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").setFocus()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").caretPosition = 3
session.findById("wnd[1]/tbar[0]/btn[8]").press()
session.findById("wnd[0]/usr/ctxtBUDAT-LOW").text = sunday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").text = saturday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").setFocus()
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/tbar[1]/btn[48]").press()
session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[2]").select()
# session.findById(
# "wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select()
session.findById("wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").select()
session.findById("wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").setFocus()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
# session.findById(
# "wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus()
# session.findById("wnd[1]/tbar[0]/btn[0]").press()
# session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = download_directory + name + "_week_" + current_day +".xls"
# session.findById("wnd[1]/usr/ctxtDY_PATH").text = download_directory
# session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = name + "_isp_week_" + current_day +".xls"
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").text = download_directory + name + "_isp_week_" + current_day +".xls"
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").caretPosition = 44
session.findById("wnd[1]/tbar[0]/btn[0]").press()
# session.findById("wnd[1]/tbar[0]/btn[0]").press()
# session.findById("wnd[1]").sendVKey(0)
else:
print('have this file')
def get_epdv_isp(session, current_day, sunday, saturday, name):
filename = download_directory + name + "_isp_week_" + current_day + ".xls"
if not os.path.exists(filename):
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb51"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtWERKS-LOW").text = name
session.findById("wnd[0]/usr/ctxtBWART-LOW").text = "Z01"
session.findById("wnd[0]/usr/ctxtBWART-LOW").setFocus()
session.findById("wnd[0]/usr/ctxtBWART-LOW").caretPosition = 3
session.findById("wnd[0]/usr/btn%_BWART_%_APP_%-VALU_PUSH").press()
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,1]").text = "Z02"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,2]").text = "601"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,3]").text = "602"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,4]").text = "Z61"
session.findById(
"wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL-SLOW_I[1,5]").text = "Z62"
# time.sleep(1000)
session.findById("wnd[1]/tbar[0]/btn[8]").press()
session.findById("wnd[0]/usr/ctxtBUDAT-LOW").text = sunday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").text = saturday
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").setFocus()
session.findById("wnd[0]/usr/ctxtBUDAT-HIGH").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/tbar[1]/btn[48]").press()
session.findById("wnd[0]/mbar/menu[0]/menu[1]/menu[2]").select()
session.findById(
"wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").select()
session.findById(
"wnd[1]/usr/sub:SAPLSPO5:0101/radSPOPLI-SELFLAG[1,0]").setFocus()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
# session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = download_directory + name + "_week_" + current_day +".xls"
session.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").text = download_directory + name + "_isp_week_" + current_day + ".xls"
# session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]").sendVKey(0)
else:
print('have this file')
def read_excel(name):
file_path = download_directory + name + "_week_" + current_day +".xls"
# 读取RFC中物料价格
xlApp = win32com.client.DispatchEx("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = 0
# 宏所在的xls
xlApp.Workbooks.Open(file_path, False)
worksheet = xlApp.ActiveSheet
# 获取总行数
row_count = worksheet.UsedRange.Rows.Count
print('row count', row_count)
m = 5
while m < row_count:
if 'UBJ' in file_path:
material = worksheet.Range("h" + str(m)).Value
plant = worksheet.Range("d" + str(m)).Value
quantity = worksheet.Range("i" + str(m)).Value
customer = worksheet.Range("ab" + str(m)).Value
elif 'HBJ' in file_path:
material = worksheet.Range("e" + str(m)).Value
plant = worksheet.Range("c" + str(m)).Value
quantity = worksheet.Range("j" + str(m)).Value
customer = worksheet.Range("y" + str(m)).Value
elif 'EPDV' in file_path:
material = worksheet.Range("c" + str(m)).Value
plant = worksheet.Range("e" + str(m)).Value
quantity = worksheet.Range("k" + str(m)).Value
customer = worksheet.Range("s" + str(m)).Value
print('material', material)
print('plant', plant)
print('quantity', quantity)
if plant and 'BIT451' not in str(customer):
item_dict = {
'plant': plant,
'quantity': quantity,
'material': material,
'customer': customer
}
item = Sap_material(**item_dict)
sql_session.add(item)
sql_session.commit()
m = m + 1
print(m)
site = 'TJ Direct ship'
items = sql_session.query(Week_data.pn, Week_data.fox_pn).filter(Week_data.site==site).distinct(Week_data.pn, Week_data.fox_pn).all()
print('len------->',len(items))
items = list(set(items))
print(len(items))
for item in items:
pn = item['pn']
fox_pn = item['fox_pn']
if fox_pn:
print('pn', pn)
print('fox_pn', fox_pn)
# time.sleep(15)
if fox_pn:
total = sql_session.query(func.sum(Sap_material.quantity)).filter(Sap_material.material == fox_pn).scalar()
if total:
total = int(total)
else:
total = 0
if total > 0:
total = 0
# 20211129 add 如果total是负数,total = -total
# 将total转为正数,存入数据库
total = -total
item_dict = {
'site': site,
'day': current_day,
'week': current_week_num,
'pn': pn,
'fox_pn':fox_pn,
'total': total
}
try:
if fox_pn:
try:
# 如果有SAP当周物料信息就更新,否则就直接插入数据
i = sql_session.query(Week_data).filter(Week_data.day==current_day,
Week_data.site==site,
Week_data.pn==pn,
Week_data.fox_pn==fox_pn).first()
print(i)
if i:
i.total = total
sql_session.commit()
print('update success')
else:
sql_session.add(Week_data(**item_dict))
sql_session.commit()
print('insert sap week data success')
except:
print('update failed')
# sql_session.rollback()
# if fox_pn:
# sql_session.add(Week_data(**item_dict))
# sql_session.commit()
except:
print('have this item')
print(item_dict)
sql_session.rollback()
xlApp.quit()
def read_excel_isp(name):
file_path = download_directory + name + "_isp_week_" + current_day +".xls"
# 读取RFC中物料价格
xlApp = win32com.client.DispatchEx("Excel.Application")
xlApp.Visible = False
xlApp.DisplayAlerts = 0
# 宏所在的xls
xlApp.Workbooks.Open(file_path, False)
worksheet = xlApp.ActiveSheet
# 获取总行数
row_count = worksheet.UsedRange.Rows.Count
print('row count', row_count)
m = 5
while m < row_count + 1:
if 'UBJ' in file_path:
material = worksheet.Range("h" + str(m)).Value
mvt = worksheet.Range("g" + str(m)).Value
plant = worksheet.Range("d" + str(m)).Value
quantity = worksheet.Range("i" + str(m)).Value
customer = worksheet.Range("ab" + str(m)).Value
elif 'HBJ' in file_path:
material = worksheet.Range("e" + str(m)).Value
mvt = worksheet.Range("f" + str(m)).Value
plant = worksheet.Range("c" + str(m)).Value
quantity = worksheet.Range("j" + str(m)).Value
customer = worksheet.Range("y" + str(m)).Value
elif 'EPDV' in file_path:
material = worksheet.Range("c" + str(m)).Value
mvt = worksheet.Range("g" + str(m)).Value
plant = worksheet.Range("e" + str(m)).Value
quantity = worksheet.Range("k" + str(m)).Value
customer = worksheet.Range("s" + str(m)).Value
print('material', material)
print('plant', plant)
print('quantity', quantity)
if plant and 'BIT451' not in str(customer) and 'BIN001' not in str(customer) and '4730600001' not in str(customer) :
item_dict = {
'plant': plant,
'quantity': quantity,
'material': material,
'customer': customer,
'mvt': mvt
}
item = Isp_sap_material(**item_dict)
sql_session.add(item)
sql_session.commit()
m = m + 1
print(m)
# site = 'TJ Direct ship'
# items = sql_session.query(Isp_week_data.site, Isp_week_data.fox_pn).filter(Isp_week_data.site==site).distinct(Isp_week_data.site, Isp_week_data.fox_pn).all()
# print('len------->',len(items))
# items = list(set(items))
# print(len(items))
# for item in items:
# site = item['site']
# fox_pn = item['fox_pn']
# if fox_pn:
# print('site', site)
# print('fox_pn', fox_pn)
# # time.sleep(15)
# if fox_pn and mvt not in ['Z61', 'Z62']:
# #料号结果相加,负数为正,正数为0
# total = sql_session.query(func.sum(Isp_sap_material.quantity)).filter(Isp_sap_material.material == fox_pn).scalar()
# if total:
# total = int(total)
#
#
# else:
# total = 0
# if total > 0:
# total = 0
# if total < 0:
# total = -total
# item_dict = {
# 'site': site,
# 'mvt': mvt,
# 'week': 'WK' + current_week_num,
# 'fox_pn':fox_pn,
# 'total': total
# }
# try:
# if fox_pn:
# try:
# i = sql_session.query(Isp_week_data).filter(Isp_week_data.week=='WK' + current_week_num,
# Isp_week_data.site==site,
# Isp_week_data.fox_pn==fox_pn).first()
# print('i ---> ', i)
# if i:
# i.total = total
# sql_session.commit()
# print('update success')
# else:
# sql_session.add(Isp_week_data(**item_dict))
# sql_session.commit()
# print('insert sap week data success')
# except:
# print('update failed')
# sql_session.rollback()
#
# except:
# print('have this item')
# print(item_dict)
# sql_session.rollback()
#
# site = 'Facebook'
# items = sql_session.query(Isp_week_data.site, Isp_week_data.fox_pn).filter(Isp_week_data.site == site).distinct(
# Isp_week_data.site, Isp_week_data.fox_pn).all()
# print('len------->', len(items))
# items = list(set(items))
# print(len(items))
# for item in items:
# site = item['site']
# fox_pn = item['fox_pn']
# if fox_pn:
# print('site', site)
# print('fox_pn', fox_pn)
# # time.sleep(15)
# if fox_pn and mvt in ['Z61', 'Z62']:
# # 料号结果相加,负数为正,正数为0
# total = sql_session.query(func.sum(Isp_sap_material.quantity)).filter(
# Isp_sap_material.material == fox_pn).scalar()
# if total:
# total = int(total)
#
#
# else:
# total = 0
# if total > 0:
# total = 0
# if total < 0:
# total = -total
# item_dict = {
# 'site': site,
# 'mvt': mvt,
# 'week': 'WK' + current_week_num,
# 'fox_pn': fox_pn,
# 'total': total
# }
# try:
# if fox_pn:
# try:
# i = sql_session.query(Isp_week_data).filter(Isp_week_data.week == 'WK' + current_week_num,
# Isp_week_data.site == site,
# Isp_week_data.fox_pn == fox_pn).first()
# print('i ---> ', i)
# if i:
# i.total = total
# sql_session.commit()
# print('update success')
# else:
# sql_session.add(Isp_week_data(**item_dict))
# sql_session.commit()
# print('insert sap week data success')
# except:
# print('update failed')
# sql_session.rollback()
#
# except:
# print('have this item')
# print(item_dict)
# sql_session.rollback()
xlApp.quit()
def excel_to_weekdata_isp():
#添加TJ Direct总数
site = 'TJ Direct ship'
items = sql_session.query(Isp_week_data.site, Isp_week_data.fox_pn, Isp_week_data.customer).filter(Isp_week_data.site==site).distinct(Isp_week_data.site, Isp_week_data.fox_pn).all()
print('len------->',len(items))
items = list(set(items))
print(len(items))
for item in items:
site = item['site']
fox_pn = item['fox_pn']
cusomter = item['customer']
if fox_pn:
print('site', site)
print('fox_pn', fox_pn)
# time.sleep(15)
if fox_pn:
#料号结果相加,负数为正,正数为0
total = sql_session.query(func.sum(Isp_sap_material.quantity)).filter(Isp_sap_material.material == fox_pn,
Isp_sap_material.mvt !='Z61', Isp_sap_material.mvt !='Z62').scalar()
if total:
total = int(total)
else:
total = 0
if total > 0:
total = 0
if total < 0:
total = -total
item_dict = {
'site': site,
'customer': cusomter,
'week': 'WK' + current_week_num,
'fox_pn':fox_pn,
'total': total
}
try:
if fox_pn:
try:
i = sql_session.query(Isp_week_data).filter(Isp_week_data.week=='WK' + current_week_num,
Isp_week_data.site==site,
Isp_week_data.fox_pn==fox_pn).first()
print('i ---> ', i)
if i:
i.total = total
sql_session.commit()
print('update success')
else:
sql_session.add(Isp_week_data(**item_dict))
sql_session.commit()
print('insert sap week data success')
except Exception as e:
print('update failed')
print(e)
sql_session.rollback()
except:
print('have this item')
print(item_dict)
sql_session.rollback()
#添加Facebook总数
customer = 'Facebook'
items = sql_session.query(Isp_week_data.site, Isp_week_data.fox_pn, Isp_week_data.customer).filter(Isp_week_data.customer == customer).distinct(
Isp_week_data.site, Isp_week_data.fox_pn).all()
print('Facebook len------->', len(items))
items = list(set(items))
print(len(items))
for item in items:
site = item['site']
fox_pn = item['fox_pn']
customer = item['customer']
if fox_pn:
print('site', site)
print('fox_pn', fox_pn)
# time.sleep(15)
if fox_pn :
# 料号结果相加,负数为正,正数为0
total = sql_session.query(func.sum(Isp_sap_material.quantity)).filter(
Isp_sap_material.material == fox_pn,
Isp_sap_material.mvt.in_(['Z61', 'Z62'])).scalar()
if total:
total = int(total)
else:
total = 0
if total > 0:
total = 0
if total < 0:
total = -total
item_dict = {
'site': site,
'customer': customer,
'week': 'WK' + current_week_num,
'fox_pn': fox_pn,
'total': total
}
try:
if fox_pn:
try:
i = sql_session.query(Isp_week_data).filter(Isp_week_data.week == 'WK' + current_week_num,
Isp_week_data.site == site,
Isp_week_data.fox_pn == fox_pn).first()
print('i ---> ', i)
if i:
i.total = total
sql_session.commit()
print('update success')
else:
sql_session.add(Isp_week_data(**item_dict))
sql_session.commit()
print('insert sap week data success')
except:
print('update failed')
sql_session.rollback()
except:
print('have this item')
print(item_dict)
sql_session.rollback()
# 添加HUB总数
site = 'HUB'
customer = 'Facebook'
items = sql_session.query(Isp_week_data.site, Isp_week_data.fox_pn, Isp_week_data.customer).filter(
Isp_week_data.customer != customer, Isp_week_data.site == site ).distinct(
Isp_week_data.site, Isp_week_data.fox_pn).all()
print('HUB len------->', len(items))
items = list(set(items))
print(len(items))
for item in items:
site = item['site']
fox_pn = item['fox_pn']
customer = item['customer']
if fox_pn:
print('site', site)
print('fox_pn', fox_pn)
# time.sleep(15)
if fox_pn:
# 直接从AWS shipping weekdata表中取total
aws_sku_list = sql_session.query(Ww.pn).filter(Ww.fox_pn.contains(fox_pn)).all()
aws_sku_list = [x[0] for x in aws_sku_list]
print(aws_sku_list)
print(len(aws_sku_list))
# time.sleep(2)
total = sql_session.query(func.sum(Web_week_data_filtered.total)).filter(
Web_week_data_filtered.pn.in_(aws_sku_list),
Web_week_data_filtered.week == current_week_num,
).scalar()
print('total', total)
time.sleep(0.3)
if total:
pass
else:
total = 0
item_dict = {
'site': site,
'customer': customer,
'week': 'WK' + current_week_num,
'fox_pn': fox_pn,
'total': total
}
try:
if fox_pn:
try:
i = sql_session.query(Isp_week_data).filter(Isp_week_data.week == 'WK' + current_week_num,
Isp_week_data.site == site,
Isp_week_data.fox_pn == fox_pn).first()
print('i ---> ', i)
if i:
i.total = total
sql_session.commit()
print('update success')
else:
sql_session.add(Isp_week_data(**item_dict))
sql_session.commit()
print('insert sap week data success')
except:
print('update failed')
sql_session.rollback()
except:
print('have this item')
print(item_dict)
sql_session.rollback()
def export_sap_xls():
'''
从SAP中导出UBJ9、HBJ9报表
:return:
'''
cwd = r'D:\code\hongmei\sap_data'
lh_sap = os.path.join(cwd,'lh_config.xls' )
tw_sap = os.path.join(cwd, 'tw_config.xls' )
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params(
lh_sap)
session = sap_login(sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time)
get_ubj9(session,current_day,sunday, saturday, 'UBJ9')
kill_sap()
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params(
tw_sap)
session = sap_login(sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time)
get_hbj9(session, current_day, sunday, saturday, 'HBJ9')
kill_sap()
def export_sap_xls_isp():
'''
从SAP中导出UBJ9、HBJ9报表
:return:
'''
cwd = r'D:\code\hongmei\sap_data'
lh_sap = os.path.join(cwd,'lh_config.xls' )
epdv_sap = os.path.join(cwd,'epdv_config.xls' )
tw_sap = os.path.join(cwd, 'tw_config.xls' )
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params(
lh_sap)
session = sap_login(sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time)
get_ubj9_isp(session,current_day,sunday, saturday, 'UBJ9')
kill_sap()
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params(
epdv_sap)
session = sap_login(sap_path, sap_version, search_name, username, password, username2, password2, query_date,
wait_time, client='801')
get_ubj9_isp(session, current_day, sunday, saturday, 'EPDV')
kill_sap()
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params(
tw_sap)
session = sap_login(sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time)
get_hbj9_isp(session, current_day, sunday, saturday, 'HBJ9')
kill_sap()
def add_isp_weekname():
try:
item = Isp_week_name(week='WK' + current_week_num)
session.add(item)
session.commit()
except:
print('have this week')
session.rollback()
# time.sleep(3)
def sap_xls_to_mysql():
'''
将导出的excel文档写入数据库
:return:
'''
name_list = ['UBJ9', 'HBJ9']
del_list = [Sap_material.__table__]
Base.metadata.drop_all(tables=del_list)
Base.metadata.create_all(tables=del_list)
for name in name_list:
read_excel(name)
def data_to_excel_isp():
add_isp_weekname()
name_list = ['UBJ9', 'EPDV', 'HBJ9']
del_list = [Isp_sap_material.__table__]
Base.metadata.drop_all(tables=del_list)
Base.metadata.create_all(tables=del_list)
for name in name_list:
read_excel_isp(name)
excel_to_weekdata_isp()
isp_to_excel()
if __name__ == '__main__':
# data_to_excel_isp()
pass
cwd = r'D:\code\hongmei\sap_data'
lh_sap = os.path.join(cwd, 'lh_config.xls')
epdv_sap = os.path.join(cwd, 'epdv_config.xls')
tw_sap = os.path.join(cwd, 'tw_config.xls')
sap_path, sap_version, search_name, username, password, username2, password2, query_date, wait_time = get_params(
lh_sap)
session = sap_login(sap_path, sap_version, search_name, username, password, username2, password2, query_date,
wait_time,client='904')
# get_ubj9_isp(session, current_day, sunday, saturday, 'UBJ9')
kill_sap()