from simple_salesforce import Salesforce import requests import pandas as pd from io import StringIO from cryptography.fernet import Fernet import os from datetime import datetime # Load the key from a specified directory directory = r'C:\Users\....' file_name = 'secret.key' file_path = f'{directory}/{file_name}' file_name_2 = 'encrypted_credentials.txt' file_path_2 = f'{directory}/{file_name_2}' # Load the key with open(file_path, 'rb') as key_file: key = key_file.read() cipher_suite = Fernet(key) # Load the encrypted credentials with open(file_path_2, 'rb') as enc_file: encrypted_username = enc_file.readline().strip() encrypted_password = enc_file.readline().strip() encrypted_security_token = enc_file.readline().strip() # Decrypt the credentials username = cipher_suite.decrypt(encrypted_username).decode() password = cipher_suite.decrypt(encrypted_password).decode() security_token = cipher_suite.decrypt(encrypted_security_token).decode() # Connect to Salesforce sf = Salesforce(username=username, password=password, security_token=security_token) print("Connected to Salesforce.") sf_instance = 'https://salesforceinstance.my.salesforce.com/' # Your Salesforce Instance URL # Define report IDs and URLs report_ids = { 'Report1': '00O8d00000587fBEAQ', 'Report2': '00O8d0000078gxGEAQ', } # Create a session and set max redirects session = requests.Session() session.max_redirects = 100 # Function to download and read a report def download_report(report_id): export = '?isdtp=p1&export=1&enc=UTF-8&xf=csv' sfUrl = sf_instance + report_id + export response = session.get(sfUrl, headers=sf.headers, cookies={'sid': sf.session_id}) if response.status_code == 302 or response.status_code == 301: print("Redirect detected. Location:", response.headers['Location']) return None else: download_report = response.content.decode('utf-8') return pd.read_csv(StringIO(download_report)) # Download and read all reports reports = {} for report_name, report_id in report_ids.items(): reports[report_name] = download_report(report_id) if reports[report_name] is not None: print(f"{report_name} report:") print(reports[report_name].to_string(index=False)) # Print the report as a table # Access the reports report_choosen = reports.get('Report1') output_table=report_choosen