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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

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
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors
Top Kudoed Authors