Skip to Content

Well, I followed Ted Ueda‘s blog posts about the RESTful Raylight Web Services and I translated his Powershell export sample to Python.

See the blog post Scripting Web Intelligence:  the RESTful Raylight Web Services for more information.

Why Python and not Powershell ?

  • Python is a cross-platform language ; you can use it on linux or unix and not only on windows ;
  • Python’s community is larger ;
  • Python is broader ; you can use it for tasks ranging from systems automation, testing, and ETL to gaming, CGI and web development.

and of course I use more Python than Powershell 😉

The original powershell script export.ps1 is here. This is the Python 3 version :

export.py

#.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))

Tested with Portable Python 3.2.5.1 on Windows XP and BI4.1 SP3.

It’s not compatible with Python 2. Syntax and Modules are different.

PS : Sorry for the misspellings … I skipped some English class hours at school 😕

To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply