因此,我有两个SAP脚本,它们都在SE16中运行一个简单的报告,并将其导出到Excel中,仅此而已。我的Python脚本严重依赖于这些数据,然而,当我回到计算机时,VBS脚本崩溃了一半时间。
尝试之间没有什么变化,有时起作用,有时不起作用,用同样的数据.
GUI脚本本身是在记录函数的帮助下完成的,在查看了生成的代码之后,我意识到我绝对不想学习这种语言(您如何计算出每个按钮的objectID,vkeys是奇怪的等等)。
有什么简单的方法可以让这个(和EKPO一样)防崩溃吗?我想我需要显示没有找到哪个对象,但是自从我开始写这篇文章,没有发生任何崩溃,它只是挂在没有错误消息.
这是Python代码的一部分,它运行VBS脚本。基于谷歌,可以在Python中直接包含VBS代码,基于下面的VBS,它会有多麻烦,它(水晶球时间)会解决我的随机崩溃吗?
def create_ausp(local_raw):
user = os.getlogin()
file = rf"C:\Users\{user}\Desktop\Reporting\AUSP.XLSX"
local_raw["Vendor"].drop_duplicates().to_clipboard(index=False)
if os.path.exists(file):
os.remove(file)
if not os.path.exists(file):
os.startfile(rf"C:\Users\{user}\AppData\Roaming\SAP\SAP GUI\Scripts\AUSP.vbs")
while not os.path.exists(file):
time.sleep(1)
if os.path.isfile(file):
ausp_headers = ["Vendor", "SAP default material group"]
df = pd.read_excel("AUSP.xlsx")
# df.drop(columns=["Internal char no."], inplace=True) #sometimes comes up in SAP, uncomment if value error happens
df.columns = ausp_headers
os.startfile(rf"C:\Users\{user}\Desktop\Reporting\AUSP close.ahk")
return dfVBS脚本:
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "se16n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[0]/usr/ctxtGD-TAB").text = "ausp"
session.findById("wnd[0]/usr/ctxtGD-TAB").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/btnPUSH[4,1]").setFocus
session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/btnPUSH[4,1]").press
session.findById("wnd[1]").sendVKey 24
session.findById("wnd[1]").sendVKey 8
session.findById("wnd[0]/tbar[1]/btn[18]").press
session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/chkGS_SELFIELDS-MARK[5,1]").selected = true
session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/chkGS_SELFIELDS-MARK[5,7]").selected = true
session.findById("wnd[0]/usr/txtGD-MAX_LINES").text = ""
session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-LOW[2,2]").text = "DEFAULT_MATERIAL_GROUP"
session.findById("wnd[0]/usr/txtGD-MAX_LINES").setFocus
session.findById("wnd[0]/usr/txtGD-MAX_LINES").caretPosition = 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 8
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").pressToolbarContextButton "&MB_VARIANT"
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/usr/ctxtDY_PATH").text = "C:\Users\...\Desktop\Reporting"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "AUSP.XLSX"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 9
session.findById("wnd[1]").sendVKey 11
session.findById("wnd[0]").sendVKey 3
session.findById("wnd[0]").sendVKey 3发布于 2022-10-24 15:34:11
我做了一些与你的代码非常相似的事情。我有一个python脚本,它首先打开SAP并登录,然后运行一个VBS脚本来下载两个excel文件,然后运行另一个python脚本来处理数据。
直到最近,我才设法将所有这些合并到一个python脚本中。虽然它仍然是VBS代码,但只有通过win32com模块嵌入到python中。我还没有找到比在SAP中记录SAP代码更好的方法,正如您所做的那样。不过,我确实发现这样做很容易,并将其合并到python中。
也许它能帮助你满足你的需要。
import psutil
import subprocess
import time
import win32com.client
from win32com import client
from win32com.client import Dispatch
if "saplogon.exe" in (i.name() for i in psutil.process_iter()):
print("SAP is running")
else:
print("SAP is not running. Starting...")
subprocess.check_call(['C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\sapshcut.exe', '-system=PE1', '-client=500', f'-user={sapuser}', f'-pw={password}', '-language=EN'])
time.sleep(20)
SapGuiAuto = win32com.client.GetObject("SAPGUI")
if not type(SapGuiAuto) == win32com.client.CDispatch:
exit
application = SapGuiAuto.GetScriptingEngine
if not type(application) == win32com.client.CDispatch:
SapGuiAuto = None
exit
connection = application.Children(0)
if not type(connection) == win32com.client.CDispatch:
application = None
SapGuiAuto = None
exit
session = connection.Children(0)
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
exit
print("Downloading excel files from SAP")
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/ncoois"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ssub%_SUBSCREEN_TOPBLOCK:PPIO_ENTRY:1100/cmbPPIO_ENTRY_SC1100-PPIO_LISTTYP").key = "PPIOO000"
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/chkP_KZ_E1").selected = True
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/chkP_KZ_E2").selected = True
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_WERKS-LOW").text = "ika1"
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtP_SYST1").text = "TECO"
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtP_SYST2").text = "CNF"
session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/chkP_KZ_E2").setFocus()
session.findById("wnd[0]").sendVKey(8)
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").setCurrentCell(-1,"")
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").selectAll()
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").contextMenu()
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").selectContextMenuItem("&XXL")
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus()
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 0
session.findById("wnd[1]").sendVKey(4)
session.findById("wnd[2]/tbar[0]/btn[12]").press()
session.findById("wnd[1]/usr/ctxtDY_PATH").text = f"C:\\users\\{user}\\Documents"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "operations.XLSX"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 15
session.findById("wnd[1]/tbar[0]/btn[11]").press()
session.findById("wnd[0]/tbar[0]/btn[15]").press()
session.findById("wnd[0]").resizeWorkingPane(267,40,False)
session.findById("wnd[0]/usr/ssub%_SUBSCREEN_TOPBLOCK:PPIO_ENTRY:1100/cmbPPIO_ENTRY_SC1100-PPIO_LISTTYP").key = "PPIOH000"
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").setCurrentCell(-1,"")
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").selectAll()
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").contextMenu()
session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").selectContextMenuItem("&XXL")
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/usr/ctxtDY_PATH").text = f"C:\\users\\{user}\\Documents"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "header.XLSX"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 12
session.findById("wnd[1]/tbar[0]/btn[11]").press()https://stackoverflow.com/questions/70260538
复制相似问题