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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

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
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.