要运行SAP数据提取脚本,我将文件的修改时间与当前时间与for循环进行比较,以确定文件是否更新。但是循环是无限的,并且不遵循for循环的条件。 我需要帮助。我正在使用python 3.6x
today = datetime.datetime.today().strftime('%d.%m.%Y')
GUIPath = 'C:\Program Files (x86)\SAP\FrontEnd\SAPgui'
SID = '-system=x'
InstanceNo = '-client=x'
User = '-user=x'
Pw = '-pw=x'
Language = '-language=x'
shell = win32com.client.Dispatch("WScript.Shell")
cmdString = os.path.join(GUIPath, 'sapshcut.exe') + " " + SID + " " + InstanceNo + " " + User + " " + Pw + " " + Language
base_sap = (r'\\teste.com\JK1\Openshare\Groups\Kb_CDO_Informacoes\Tableau\Pedidos_Ingressados\Files\Teste\base_sap.xlsx')
now = datetime.datetime.now().timestamp()
for f in base_sap:
while os.stat(f).st_mtime < (now - 1800):
print('Iniciando atualização da Base de Pedidos do SAP.')
#inicia o sap logon
call(cmdString)
time.sleep(4)
os.system('TASKKILL /F /IM saplogon.exe')
call(cmdString)
time.sleep(10)
#iniciando a sessão no sap gui
SapGui = win32com.client.GetObject("SAPGUI").GetScriptingEngine
session = SapGui.FindById("ses[0]")
time.sleep(8)
#sap gui script
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode("F00002")
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_VKORG-LOW").text = "BR03"
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_VKORG-HIGH").text = "BR04"
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_WERKS-LOW").text = "*"
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_AUART-LOW").text = "ZOR"
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_ERDAT-LOW").text = today
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_ERDAT-HIGH").setFocus()
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_ERDAT-HIGH").caretPosition = 0
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003").select()
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0300/ctxtP_UNIT").text = "L"
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0300/ctxtP_UNIT").setFocus()
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0300/ctxtP_UNIT").caretPosition = 1
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002").select()
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/ctxtP_VAR").text = "PEDIDOS_INGR"
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/txtP_TITLE").text = ""
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/txtP_TITLE").setFocus()
session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/txtP_TITLE").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[8]").press()
#exporta os dados em xlsx via automação pyautogui
time.sleep(6)
pyautogui.click(15, 32)
pyautogui.click(57, 106)
pyautogui.click(291, 126)
time.sleep(6)
pyautogui.press('enter')
time.sleep(14)
#encerra a aplicação excel
os.system('TASKKILL /F /IM EXCEL.EXE')
#encerra a aplicação sap logon
os.system('TASKKILL /F /IM saplogon.exe')
#copia o arquivo para o diretório compartilhado
shutil.move(r"C:\Users\%s\Documents\SAP\export.XLSX" % os.getlogin(), r"\\teste.com\JK1\Openshare\Groups\Kb_CDO_Informacoes\Tableau\Pedidos_Ingressados\Files\Teste\base_sap.xlsx")
else:
print('Base atualizada. Iniciando upload para o Tableau')