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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register 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
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.