Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download an SSRS report in Python using requests

I am trying to use requests to download an SSRS report. The following code will download an empty Excel file:

url = 'http://MY REPORT URL HERE/ReportServer?/REPORT NAME HERE&rs:Format=EXCELOPENXML'

s = requests.Session()
s.post(url, data={'_username': 'username, '_password': 'password'})

r = s.get(url)

output_file = r'C:\Saved Reports\File.xlsx'

downloaded_file = open(output_file, 'wb')
for chunk in r.iter_content(100000):
    downloaded_file.write(chunk)

I have successfully used requests_ntlm to complete this task, but I am wondering why the above code is not working as intended. The Excel file turns out to be empty; I feel it is due to an issue with logging in and passing those cookies to the GET request.

like image 636
Sultan of Swing Avatar asked Mar 19 '26 09:03

Sultan of Swing


1 Answers

I was able to get this to work, but for pdfs. I found the solution here

Here's a piece of my code snippet:

import requests
from requests_ntlm import HttpNtlmAuth

session = requests.Session()
session.auth = HttpNtlmAuth(domain+uid,pwd)
response = session.get(reporturl,stream=True)
print response.status_code
with open(outputlocation+mdcProp+'.pdf','wb') as pdf:
    for chunk in response.iter_content(chunk_size=1024):
        if chunk:
            pdf.write(chunk)
session.close()
like image 81
Drew Avatar answered Mar 20 '26 21:03

Drew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!