-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3632c91
commit c2cf686
Showing
2 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# SAP Login Script | ||
import win32com.client | ||
import subprocess | ||
import time | ||
import os | ||
import tkinter as tk | ||
from tkinter import filedialog, messagebox | ||
|
||
|
||
def get_sap_client_session(my_username, my_password, my_sap_sid="SB2", my_sap_sysname='"S4 PRD [Wiwynn S4 PRD]"', my_sap_client="999"): | ||
try: | ||
sap_gui = win32com.client.GetObject("SAPGUI") | ||
application = sap_gui.GetScriptingEngine | ||
connection = application.Children(0) | ||
|
||
except: | ||
# Can not find the GUI window, so utilize sapshcut to open | ||
subprocess.check_call('start "C:/Program Files (x86)/SAP/FrontEnd/SAPgui/" sapshcut.exe -system=' + | ||
my_sap_sid + ' -sysname=' + my_sap_sysname + ' -user=' + my_username + | ||
' -pw=' + my_password + ' -client=' + my_sap_client, shell=True) | ||
|
||
# Make sure the login window is open | ||
time.sleep(10) | ||
sap_gui = win32com.client.GetObject("SAPGUI") | ||
if not type(sap_gui) == win32com.client.CDispatch: | ||
return | ||
|
||
application = sap_gui.GetScriptingEngine | ||
if not type(application) == win32com.client.CDispatch: | ||
sap_gui = None | ||
return None | ||
|
||
connection = application.Children(0) | ||
if not type(connection) == win32com.client.CDispatch: | ||
application = None | ||
sap_gui = None | ||
return None | ||
|
||
session = connection.Children(0) | ||
if not type(session) == win32com.client.CDispatch: | ||
connection = None | ||
application = None | ||
sap_gui = None | ||
|
||
# Get session | ||
try: | ||
session = connection.Children(0) | ||
# If password and account is not corrrect | ||
if ("Name or password is incorrect" in session.findById("wnd[0]/sbar").Text): | ||
print('Name or password is incorrect') | ||
return None | ||
|
||
# Handle duplicated login | ||
if (session.children.count > 1): | ||
try: | ||
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").select() | ||
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").setFocus() | ||
session.findById("wnd[0]").sendVKey(0) | ||
except: | ||
# print('Can not login to SAP successfully.') | ||
# session = None | ||
pass | ||
if (session.children.count > 1): | ||
try: | ||
session.findById("wnd[1]").close() | ||
except: | ||
session = None | ||
return session | ||
except: | ||
print('Cannot get SAP GUI client sucessfully') | ||
return None | ||
|
||
|
||
def choose_file(): | ||
# 隐藏主窗口 | ||
root = tk.Tk() | ||
root.withdraw() | ||
|
||
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")]) | ||
|
||
return file_path | ||
|
||
|
||
def read_credentials(file_path): | ||
# Read credentials from file | ||
try: | ||
with open(file_path, "r") as f: | ||
credentials = f.readlines() | ||
username = credentials[0].strip() | ||
password = credentials[1].strip() | ||
except Exception as e: | ||
print("Error reading credentials: ", e) | ||
return None, None | ||
return username, password | ||
|
||
|
||
def main(): | ||
credentials_file = choose_file() | ||
if credentials_file: | ||
username, password = read_credentials(credentials_file) | ||
get_sap_client_session(username, password) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import win32com.client | ||
import sys | ||
import subprocess | ||
import time | ||
import os | ||
|
||
|
||
class ReadTXT(): | ||
def __init__(self, filepath): | ||
self.filepath = filepath | ||
|
||
def read_credentials(self): | ||
try: | ||
with open(self.filepath, 'r') as file: | ||
lines = file.readlines() | ||
username = lines[0].strip() # first line is username | ||
password = lines[1].strip() # second line is password | ||
return username, password | ||
except Exception as e: | ||
print(f"读取文件时发生错误: {e}") | ||
return None, None | ||
|
||
|
||
class SapGui(): | ||
def __init__(self): | ||
self.path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe" | ||
subprocess.Popen(self.path) | ||
time.sleep(5) | ||
|
||
self.SapGuiAuto = win32com.client.GetObject("SAPGUI") | ||
if not type(self.SapGuiAuto) == win32com.client.CDispatch: | ||
return | ||
|
||
application = self.SapGuiAuto.GetScriptingEngine | ||
self.connection = application.OpenConnection( | ||
"S4 PRD [Wiwynn S4 PRD]", True) | ||
self.session = self.connection.Children(0) | ||
self.session.findById("wnd[0]").maximize() | ||
|
||
def saplogin(self, username, password): | ||
|
||
try: | ||
self.session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "999" | ||
self.session.findById("wnd[0]/usr/txtRSYST-BNAME").text = username | ||
self.session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = password | ||
self.session.findById("wnd[0]").sendVKey(0) | ||
|
||
except Exception as e: | ||
print(f"登入SAP時發生錯誤: {e}") | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
downloads_path = os.path.join(os.path.expanduser( | ||
'~'), 'Downloads') | ||
credentials_file = os.path.join(downloads_path, "SAP Login.txt") | ||
reader = ReadTXT(credentials_file) | ||
username, password = reader.read_credentials() | ||
sap = SapGui() | ||
sap.saplogin(username, password) |