gpt4 book ai didi

python-3.x - 如何使用python操作SAP Gui并运行SAP Scripts?

转载 作者:行者123 更新时间:2023-12-03 23:14:04 24 4
gpt4 key购买 nike

作为 SAP MM 的官员,我想编写一个应用程序来做一些重复的工作。但是当我录制 SAP Gui Script 时,我无法在 Python 中成功运行它。我是否使用了错误的模块或我的代码有问题?

import win32com.client
import sys

def SAP_OP():
excelPath = r'#'
SapGuiAuto = win32com.client.GetObject("SAPGUI")
if not type(SapGuiAuto) == win32com.client.CDispatch:
return

application = SapGuiAuto.GetScriptingEngine
if not type(application) == win32com.client.CDispatch:
SapGuiAuto = None
return

connection = application.Children(0)
if not type(connection) == win32com.client.CDispatch:
application = None
SapGuiAuto = None
return

session = connection.Children(0)
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nMM03"
session.findById("wnd[0]").sendVKey(0)
session.findById("wnd[0]/usr/ctxtS_VKORG-LOW").text = "#"
session.findById("wnd[0]/usr/ctxtS_VTWEG-LOW").text = "#"
session.findById("wnd[0]/usr/ctxtP_EKORG").text = "#"
session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").text = "#"
session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").setFocus
session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").caretPosition = 4
session.findById("wnd[0]/tbar[1]/btn[8]").press()
session.findById("wnd[0]/usr/cntlMAT_CONT/shellcont/shell").currentCellColumn = r"#"
session.findById("wnd[0]/usr/cntlMAT_CONT/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlMAT_CONT/shellcont/shell").selectContextMenuItem = r"&XXL"
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/tbar[0]/btn[0]").press()
session.findById("wnd[1]/tbar[0]/btn[0]").press()

session = None
connection = None
application = None
SapGuiAuto = None

SAP_OP()

最佳答案

试试下面它应该工作。根据您的凭据更改 ConnectionName、用户名和密码。

# Importing the Libraries
import win32com.client
import sys
import subprocess
import time


# This function will Login to SAP from the SAP Logon window

def saplogin():

try:

path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
subprocess.Popen(path)
time.sleep(10)

SapGuiAuto = win32com.client.GetObject('SAPGUI')
if not type(SapGuiAuto) == win32com.client.CDispatch:
return

application = SapGuiAuto.GetScriptingEngine
if not type(application) == win32com.client.CDispatch:
SapGuiAuto = None
return
connection = application.OpenConnection("ConnectionName", True)

if not type(connection) == win32com.client.CDispatch:
application = None
SapGuiAuto = None
return

session = connection.Children(0)
if not type(session) == win32com.client.CDispatch:
connection = None
application = None
SapGuiAuto = None
return

session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "USERNAME"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD"
session.findById("wnd[0]").sendVKey(0)

except:
print(sys.exc_info()[0])

finally:
session = None
connection = None
application = None
SapGuiAuto = None


saplogin()

关于python-3.x - 如何使用python操作SAP Gui并运行SAP Scripts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54086565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com