Skip to content

Instantly share code, notes, and snippets.

@clintcarr
Last active December 22, 2022 19:14
Show Gist options
  • Save clintcarr/a4eef73831e090f052569048dbdd29fa to your computer and use it in GitHub Desktop.
Save clintcarr/a4eef73831e090f052569048dbdd29fa to your computer and use it in GitHub Desktop.
Simple Python script to generate Qlik Sense report and download it.
import requests
import time
from requests.structures import CaseInsensitiveDict
url = "https://<tenant>.<region>.qlikcloud.com/api/v1/reports"
headers = CaseInsensitiveDict()
headers["Authorization"] = "Bearer <id>"
headers["Content-type"] = "application/json"
data = '{"type":"composition-1.0","output":{"type":"pdfcomposition","outputId":"composition1","pdfCompositionOutput":{"pdfOutputs":[{"size":"A4","align":{"vertical":"middle","horizontal":"center"},"resizeType":"autofit","orientation":"A"},{"size":"A4","align":{"vertical":"middle","horizontal":"center"},"resizeType":"autofit","orientation":"A"}]}},"definitions":{"selectionsByState":{"sel1":{"$":[{"values":[{"text":"Border","isNumeric":false}],"fieldName":"Regio","defaultIsNumeric":false}]}}},"compositionTemplates":[{"type":"sense-sheet-1.0","senseSheetTemplate":{"selectionStrategy": "failOnErrors","appId":"88d3b0dd-ffa8-4fd2-a21e-a73101e71bb0","sheet":{"id":"b48ff826-85e9-440c-8334-24e8199fa136"},"selectionsByStateDef":"sel1"}},{"type":"sense-sheet-1.0","senseSheetTemplate":{"selectionStrategy": "failOnErrors","appId":"88d3b0dd-ffa8-4fd2-a21e-a73101e71bb0","sheet":{"id":"HDyxXL"},"selectionsByStateDef":"sel1"}}]}'
def requestReport():
print ("Requesting Report generation..")
resp = requests.post(url, headers=headers, data=data)
return (resp.headers['Location'])
def reportStatus(reportId):
del headers['Content-type']
status = ''
while not (status == 'done') :
resp = requests.get(reportId, headers=headers)
data = resp.json()
status = data['status']
print (data['status'])
time.sleep (1.5)
print ('Report created and ready..')
return (data['results'][0]['location'])
def downloadReport(url):
local_filename = 'report.pdf'
with requests.get(url, headers=headers,stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return local_filename
if __name__ == '__main__':
location = requestReport()
downloadReport(reportStatus(location))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment