0

I've been searching around the site for the past week or two and there seem to be a lot of opinions and ideas on how to download a report into a DataFrame from SF using Python (simple-salesforce & salesforce-reporting) but none provide a proper and actionable answer.

So for my self and everyone who will need this in the future I'd love to know if theres a concrete answer to the question: can you download a report from salesforce using python?

If not, does anyone have a proper recommendation of how to proceed using SOQL?

Thanks!

5
  • 2
    Does this answer your question? simple_salesforce Run Report – identigral Dec 3 '20 at 17:35
  • 1
    Downloading a report (i.e., in "XLS" or CSV format) and running a report (through the Analytics API, with response body in JSON) are very different. Could you edit to clarify what you are trying to do and where the specific problem is located? – David Reed Dec 3 '20 at 17:52
  • @identigral i found that before but it's not immediately obvious how to parse out the data contained in the datacells – Gorlomi Dec 3 '20 at 18:07
  • @DavidReed Updated! your answer here is brilliant but i'm not sure (and im sure a lot of other people are unsure as well) as to how to parse the incoming data from those datacells – Gorlomi Dec 3 '20 at 18:09
  • 1
    @Gorlomi What have you tried? Where are you stuck? The Salesforce portion of this problem is covered in David's answer, here's another example stackoverflow.com/questions/57575852/… . The rest is just writing code in Python. – identigral Dec 3 '20 at 18:19
0

I actually found the answer in a website I can't find again but luckily I saved the code locally.

For anyone just trying to download a report into a DataFrame this is how you do it (I added some notes and links for clarifications):

import pandas as pd
import csv
import requests
from io import StringIO
from simple_salesforce import Salesforce

# Input Salesforce credentials:
sf = Salesforce(
    username='johndoe@mail.com', 
    password='<password>', 
    security_token='<security_token>') # See below for help with finding token 

# Basic report URL structure:
orgParams = 'https://<INSERT_YOUR_COMPANY_NAME_HERE>.my.salesforce.com/' # you can see this in your Salesforce URL
exportParams = '?isdtp=p1&export=1&enc=UTF-8&xf=csv'

# Downloading the report:
reportId = 'reportId' # You find this in the URL of the report in question between "Report/" and "/view"
reportUrl = orgParams + reportId + exportParams
reportReq = requests.get(reportUrl, headers=sf.headers, cookies={'sid': sf.session_id})
reportData = reportReq.content.decode('utf-8')
reportDf = pd.read_csv(StringIO(reportData))

You can get your token by following the instructions at the bottom of this page

Not the answer you're looking for? Browse other questions tagged or ask your own question.