#.Synopsis
#Exports WebI to PDF.
#.Description
#Logs onto BI Platform, retrieves PDF export of a Web Intelligence document and save to specified file.
#Modify this script to enter the logon credentials, URL to the RESTful Web Services, Language, Document SI_ID and folder path.
#
import urllib.request
import urllib.parse
import json
import os
############################################################################################################################
# Input: logonInfo, hostUrl, locale, documentId and folderPath to suite your preferences
logonInfo = {
'userName' : 'administrator',
'password' : 'Password1',
'auth' : 'secEnterprise'
}
hostUrl = 'http://10.160.206.89:6405/biprws'
documentId = '7827' # SI_ID for the document
locale = 'en-US' # Format language for the WebI exporter
contentLocale = 'en-US' # Format language for the WebI document contents
folderPath = r'C:\Users\I819039\Desktop\RESTful' # Folder where PDF file will be saved.
############################################################################################################################
raylightUrl = hostUrl + '/raylight/v1'
documentUrl = raylightUrl + '/documents/' + documentId
# Logon and retrieve the logon token to be used in subsequent RESTful calls.
headers = {
'Content-Type' : 'application/json',
'Accept' : 'application/json',
}
d=str.encode(json.dumps(logonInfo))
result = urllib.request.urlopen(urllib.request.Request(hostUrl + "/logon/long",d,headers))
reponse=json.loads(result.read().decode('utf-8'))
logonToken=reponse['logonToken']
# Get Web Intelligence document information.
headers = {
'X-SAP-LogonToken' : '"'+logonToken+'"' ,
'Accept' : 'application/json',
'Content-Type' : 'application/json',
'Accept-Language' : locale,
'X-SAP-PVL' : contentLocale
}
result = urllib.request.urlopen( urllib.request.Request(documentUrl,None,headers) )
reponse=json.loads(result.read().decode('utf-8'))
document = reponse['document']
# Refresh the document by sending empty prompts (assumes document has no prompts).
headers = {
'X-SAP-LogonToken' : '"'+logonToken+'"' ,
'Accept' : 'application/json' ,
'Content-Type' : 'application/json' ,
'X-SAP-PVL' : contentLocale
}
parametersUrl = documentUrl + '/parameters'
urllib.request.urlopen( urllib.request.Request(parametersUrl,None,headers) )
# Retrieve and save PDF first ensuring the file path is valid.
filePath = os.path.join(folderPath , document['name'] + '.pdf')
if( os.access(os.path.dirname(filePath), os.W_OK) ) :
# Get PDF and save to file
headers = {
'X-SAP-LogonToken' : '"'+logonToken+'"' ,
'Accept' : 'application/pdf' ,
'X-SAP-PVL' : contentLocale
}
result = urllib.request.urlopen( urllib.request.Request(documentUrl + '/pages',None,headers) )
f = open(filePath, 'wb')
f.write(result.read())
f.close()
else :
print ('Invalid file path ' + filePath)
# Unload document from Raylight.
headers = {
'X-SAP-LogonToken' : '"'+logonToken+'"' ,
'Accept' : 'application/json' ,
'Content-Type' : 'application/json' ,
'X-SAP-PVL' : contentLocale
}
data = {
'document' : { 'state' : 'Unused' }
}
d=str.encode(json.dumps(data))
urllib.request.urlopen(urllib.request.Request(documentUrl,d,headers))
# Log off the Session identified by the X-SAP-LogonToken HTTP Header
headers = {
'X-SAP-LogonToken' : '"'+logonToken+'"' ,
'Accept' : 'application/json' ,
'Content-Type' : 'application/json'
}
urllib.request.urlopen(urllib.request.Request(hostUrl + '/logoff',b'',headers))
|
Wonderful 🙂
Thanks 😎
Excellent stuff 🙂
I just Tweeted this from https://twitter.com/SAPCRNetSup
Many thanks for the contribution!
- Ludek
Senior Support Engineer AGS Product Support, Global Support Center Canada
arrgh awesome but i use python 2.7 mainly. Maybe i should take time to translate it into 2.7
Consider using the Requests library instead of urllib2, it will drastically cut the amount of code you need to write to perform HTTP requests and is considerably easier to use.
Interesting 😀
I didn't know this external library.
And it seems working in Python 2 and 3 !
Hi all,
Great work !
Someone did the same in php?
Regards,
Sassoun
Is it possible to get and save to file list of documents etc?
This is great. Are there any sites where people share similar code? I'm new to Python and having base code to build off and/or reverse engineer is extremely helpful. My ultimate goal is to be able to extract the details of universes and Webi (including query details). Our vendor has frequent data model changes/additions and we'd like to simplify the impact analysis process.
Hi Michael Gaynor,
Did you find any solution for the requirement that you had ?
Regards
MK