This post contains Python code to retrieve and download attachments from Zoho CRM Deals. It uses the Zoho CRM API to fetch attachment information and download the files. It cab be applied for other modules of Zoho as well
Setup
First, let's import the necessary libraries:
import requests
import json
import pandas as pd
import os
Reading CRM Deals Data
We start by reading a CSV file containing CRM Deals information:
csv = pd.read_csv('CRMDeals.csv')
print(csv)
This will display the contents of the CSV file, which includes deal IDs and account names.
Retrieving Attachments
The following code block retrieves attachments for each deal and attempts to download them:
token = '#Token#'
for x in csv['id']:
r1 = requests.get(
url=f'https://www.zohoapis.com/crm/v2/Deals/{x}/Attachments',
headers={
"orgId": "#OrgID#",
"Authorization": f"Zoho-oauthtoken {token}"
}
)
print(r1.json())
try:
j1 = r1.json()
df1 = pd.DataFrame.from_dict(j1["data"])
for y in df1['id']:
r2 = requests.get(
url=f'https://www.zohoapis.com/crm/v2/Deals/{x}/Attachments/{y}',
headers={
"orgId": "657802527",
"Authorization": f"Zoho-oauthtoken {token}"
}
)
open(f"{y}_{df1['account_name'][y]}_Contract.pdf", 'wb').write(r2.content)
except Exception as e:
print(type(e))
This code does the following:
- Iterates through each deal ID in the CSV file.
- Retrieves the list of attachments for each deal.
- For each attachment, it downloads the file and saves it with a name that includes the attachment ID and account name.
Notes
- Make sure to replace the
token
variable with a valid Zoho CRM API token. - The script assumes that the attachments are PDF files. Modify the file extension if dealing with other file types.
- Error handling is minimal in this script. In a production environment, you'd want to add more robust error checking and logging.
- Be mindful of API rate limits when running this script on a large number of deals.
Remember to handle your API tokens securely and not expose them in public repositories or shared notebooks.