Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing salesforce report data using python

I am new to sfdc . I have report already created by user . I would like to use python to dump the data of the report into csv/excel file. I see there are couple of python packages for that. But my code gives an error

from simple_salesforce import Salesforce
sf = Salesforce(instance_url='https://cs1.salesforce.com', session_id='')
sf = Salesforce(password='xxxxxx', username='xxxxx', organizationId='xxxxx')

Can i have the basic steps for setting up the API and some example code

like image 532
user3367015 Avatar asked Feb 14 '23 07:02

user3367015


1 Answers

This worked for me:

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


sf = Salesforce(username=your_username, password=your_password, security_token = your_token)


login_data = {'username': your_username, 'password': your_password_plus_your_token}


with requests.session() as s:
    d = s.get("https://your_instance.salesforce.com/{}?export=1&enc=UTF-8&xf=csv".format(reportid), headers=sf.headers, cookies={'sid': sf.session_id})

d.content will contain a string of comma separated values which you can read with the csv module.

I take the data into pandas from there, hence the function name and import pandas. I removed the rest of the function where it puts the data into a DataFrame, but if you're interested in how that's done let me know.

like image 193
Obol Avatar answered Feb 16 '23 01:02

Obol