2

Hi I connected to the salesforce using simple_salesforce module and targeted it to the report i wanted to extract. At this moment I have the desired data in csv comma seperated format and when I write d.content it shows the data in comma separated but I want to have the same data in pandas dataframe.

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

then

in[22]: d.content

out[22]: "whole comma seperated data"

I want the above data to be in pandas or saved in csv

when I write:

pd.DataFrame(d)

it gives an error out as

PandasError: DataFrame constructor not properly called!

Please tell me how can I take it further to save the following data in either csv or insert in pandas to save it further.

Maneet Giri
  • 185
  • 3
  • 18

2 Answers2

7

I know this is more detailed than you need but as someone who searched all over for a full answer for how to download salesforce reports to excel using python I wanted to share my answer:

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

# login info must be a string in "quotes" 
sf = Salesforce(username= # "UserName"
                ,password= # "Password" 
                ,security_token= # "Security Token" (can be reset in your settings in salesforce and will be sent via email)
                )

sid=sf.session_id
print ("get sid "+ sf.session_id)


#ReportID can be found when you open the report in the browser address bar as follows: https://instance.salesforce.com/[Alphanumeric ReportID]
ReportID=  #Alphanumeric Report ID as string in "quotes"

response = requests.get("https://instance.salesforce.com/"+ReportID+"?export=1&enc=UTF-8&xf=csv",
                  headers = sf.headers, cookies = {'sid' : sid})

df=pd.read_csv(io.StringIO(response.text))
df=df[:-5] #takes out the confidential information warning and copywrite info

#if your first column is a number field instead of string (text) field you will need the next line(Delete the '#')
#df=df.astype({"[first column name]":'float'})


writer = pd.ExcelWriter('Salesforce Output.xlsx') #this will write the file to the same folder where this program is kept
df.to_excel(writer,index=False,header=True)

writer.save()

I made the comments very simple so that people who do not regularly use python or beginners can learn from my experience.

torpy
  • 276
  • 5
  • 14
3

The data returned by the server is contained in the text attribute of the response: d.text (see documentation here).

You can then use the answer here to create the dataframe from a string:

from StringIO import StringIO
pd.DataFrame.from_csv(StringIO(d.text))
Community
  • 1
  • 1
IanS
  • 15,771
  • 9
  • 60
  • 84