Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
moonsarinalu
Frequent Visitor

Save Pdf format of your report using Python

I would like to access to a report in powerbi server and save the report in pdf format in my PC using python, but I recived a message as

"the result should be passed only to trusted code in your notebook."

Here is the code I used:

import requests
from powerbiclient.authentication import DeviceCodeLoginAuthentication

# Authenticate and get the access token
device_auth = DeviceCodeLoginAuthentication()
access_token = device_auth.get_access_token()

group_id = "XXX-XXX-XXX-XXX-XXXX"
report_id = "YYY-YYY-YYY-YYY-YYY"

# API endpoint for exporting report to PDF
url = f"https://api.powerbi.com/v1.0/myorg/groups/{group_id}/reports/{report_id}/ExportTo"

# Request headers
headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json"
}

# Request body specifying the export settings
data = {
    "format": "pdf"
}

# Send POST request to export report to PDF
response = requests.post(url, headers=headers, json=data)

# Check the response status code
if response.status_code == 202:
    # Get the URL for the exported PDF file
    response_data = response.json()
    download_url = response_data["downloadLocation"]
   
    # Download the PDF file
    response = requests.get(download_url)
   
    # Save the PDF file to the specified path
    pdf_file_path = "./report.pdf"
    with open(pdf_file_path, "wb") as f:
        f.write(response.content)
       
    print("Report exported to PDF successfully.")
else:
    print("Exporting report to PDF failed.")
2 REPLIES 2
moonsarinalu
Frequent Visitor

Hello @apenaranda 

unfortunatelly not. I could never run it, and am thinking of using other solutions other than power bi to export reports.

apenaranda
Post Partisan
Post Partisan

Hello @moonsarinalu 

 

Did you get it to work?
I get an error on the line

download_url = response_data["downloadLocation"]

 

apenaranda_0-1710249531479.png

 

Helpful resources

Announcements
Fabric Data Days is here Carousel

Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.