使用请求在Python中下载SSRS报告

时间:2019-04-24 00:22:02

标签: python reporting-services python-requests

我正在尝试使用requests下载SSRS报告。以下代码将下载一个空的Excel文件:

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)

我已经成功地使用requests_ntlm完成了此任务,但是我想知道为什么上面的代码无法按预期工作。 Excel文件原来是空的。我认为这是由于登录并将这些Cookie传递到GET请求而引起的。

1 个答案:

答案 0 :(得分:0)

我能够使它工作,但对于pdfs。我找到了解决方法here

这是我的一段代码:

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()