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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. 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

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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