Introduction
Connecting Python to Salesforce via ODBC enables seamless interaction with CRM data—retrieving leads, managing accounts, and automating business processes directly from your Python applications. This guide walks you through the installation, configuration, and execution steps required for a reliable integration using the pyodbc
module alongside the ZappySys Salesforce ODBC Driver.
In this tutorial, you’ll learn how to connect to the Salesforce API using Python via ODBC, leveraging the powerful capabilities of the ZappySys ODBC PowerPack.
Prerequisites
- Download and install the ZappySys ODBC PowerPack.
- A Salesforce account.
Steps
Step 1: Install pyodbc
in Python
-
Ensure Python is installed. If not, download it from the official website.
-
Open your terminal or command prompt and run:
python -m pip install pyodbc
This installs the
pyodbc
library, which enables Python to interact with ODBC drivers.
Step 2: Generate your Salesforce security token
-
Log in to Salesforce.
-
Click your profile icon, go to Settings > Personal Information > Reset My Security Token, and click Reset Security Token.
-
Check your email inbox—your token will be sent there.
Step 3: Set up the ODBC Data Source
-
Open the ODBC Data Source Administrator by searching for “ODBC” on your system.
-
Under the User DSN tab, click Add and choose ZappySys Salesforce Driver.
-
In the connection settings, enter your Salesforce username, password, and security token, then click Test Connection.
-
Go to the Preview tab, select a table, and click Preview Data to confirm the connection.
Step 4: Read Salesforce data in Python
Create a script named salesforce_example.py
:
import pyodbc
def fetch_salesforce_data():
try:
# In connection string use the same data source name we used in the previous step
conn = pyodbc.connect('DSN=ZappySys Salesforce Driver')
cursor = conn.cursor()
print("------------ Salesforce & Python ----------------")
cursor.execute("SELECT * FROM User")
rows = cursor.fetchall()
columns = [column[0] for column in cursor.description]
# Iterate through each row and print as a dictionary
for row in rows:
row_dict = dict(zip(columns, row))
# Example output:
# {'Id': '005...', 'Username': 'jsmith', 'Email': 'jsmith@example.com'}
print(row_dict)
except pyodbc.Error as e:
print("Error during connection or query execution:", e)
finally:
try:
cursor.close()
conn.close()
except:
pass
if __name__ == "__main__":
fetch_salesforce_data()
Run the script to see your Salesforce records printed in the console:
Step 5: Use a whole Connection String (Optional)
To avoid setting up DSNs manually, you can use a connection string directly:
- Click Copy Settings in the ODBC UI after configuring your DSN.
- Replace the
DSN=...
in your script with the copied string:
conn = pyodbc.connect('DRIVER={ZappySys Salesforce Driver};User=you@example.com;Password=your_password+token;...')
Video tutorial
Conclusion
Following this step-by-step guide, you can successfully integrate Python with Salesforce using the ZappySys Salesforce ODBC Driver. This method allows you to automate workflows, retrieve and process CRM data, and perform analytics using Python. Download, explore the ZappySys ODBC PowerPack and get going!
References
- Salesforce ODBC Driver
- Salesforce ODBC Driver documentation
- Salesforce Connector | API Integration Hub
- Blog articles
- ODBC PowerPack
Contact us
If you encounter any issues or have specific questions, reach out to our support team via live chat or support ticket.