Warm tip: This article is reproduced from stackoverflow.com, please click
python sap sap-gui

Login to SAP session by Python

发布于 2020-04-08 23:45:06

I used this script to login SAP session:

from subprocess import call
import win32com.client
import time
import os

GUIPath = 'C:/Program Files (x86)/SAP/FrontEnd/SAPgui/'
WinTitle = 'SAP'
Name = """PRD"""
SID = 'PRD'
InstanceNo = '01'

shell = win32com.client.Dispatch("WScript.Shell")
call(os.path.join(GUIPath, 'SAPgui.exe') + " " + Name + " " + InstanceNo)

however, it always return with the error:

hostname 'PRD' unknown
check you application server name

anyone knows how to fix this? thanks

Questioner
Tommy Thang
Viewed
97
7,079 2020-02-02 18:02

The variable Name needs to be "PRD" (the string itself must contain double quotes). Python considers Name = """PRD""" the same as Name = "PRD" so it's incorrect because the variable Name will just contain PRD (missing double quotes).

Hence, need to use string backslash (Name = "\"PRD\"" or other possibilities mentioned here) to maintain the double quote in the variable Name.

Complete code:

from subprocess import call
import win32com.client
import time
import os

GUIPath = 'C:/Program Files (x86)/SAP/FrontEnd/SAPgui/'
WinTitle = 'SAP'
Name = "\"PRD\""
SID = 'PRD'
InstanceNo = '01'

shell = win32com.client.Dispatch("WScript.Shell")
call(os.path.join(GUIPath, 'SAPgui.exe') + " " + Name + " " + InstanceNo)