@@ -21,6 +21,7 @@ A powerful CLI and GUI tool to export Salesforce SOQL query results to local CSV
2121- [ Usage] ( #usage )
2222 - [ CLI Usage] ( #cli-usage )
2323 - [ GUI Usage] ( #gui-usage )
24+ - [ Library Usage] ( #library-usage )
2425- [ Post-processing] ( #post-processing )
2526- [ Examples] ( #examples )
2627- [ Troubleshooting] ( #troubleshooting )
@@ -52,7 +53,8 @@ pip install -e "git+https://github.com/datsom1/soql2csv.git#egg=soql2csv[dev]"
5253soql2csv requires Salesforce authentication credentials in a ` .env ` file.
5354
5455Example ` .env ` file:
55- ```
56+
57+ ``` env
5658SF_USERNAME=your_salesforce_username
5759SF_PASSWORD=your_salesforce_password
5860SF_SECURITY_TOKEN=your_salesforce_security_token
@@ -72,6 +74,7 @@ soql2csv [OPTIONS] SOQLFILE
7274```
7375
7476Get help:
77+
7578``` bash
7679soql2csv --help
7780```
@@ -99,7 +102,7 @@ soql2csv --help
99102soql2csv path/to/query.soql --outfilename=Extract_Table__c.csv --outfilepath=./output
100103```
101104
102- ** CLI Preview**
105+ ### CLI Preview
103106
104107![ soql2csv v2 2 CLI helptext] ( https://github.com/user-attachments/assets/a630de3d-d0d3-4c12-a2f2-80129d39f157 )
105108
@@ -113,6 +116,7 @@ soql2csv --gui
113116```
114117
115118The GUI provides fields for:
119+
116120- SOQL File: Select your query file
117121- Output Folder: Where exported CSVs will be saved
118122- Env File: Path to your .env file with Salesforce credentials
@@ -121,13 +125,82 @@ The GUI provides fields for:
121125
122126A console output area displays progress and results.
123127
124- ** Gui Preview**
128+ ### GUI Preview
125129
126130![ soql2csv v2 2 GUI] ( https://github.com/user-attachments/assets/a0a18088-65ff-4f06-b0a7-cc5fc3a52d4e )
127131
132+ ## Library Usage
133+
134+ You can also call ` soql2csv ` directly from Python code without invoking the CLI.
135+
136+ Primary helper:
137+
138+ ``` text
139+ export_soql_to_csv(soql_path, output_base_name, output_dir='.', env_path='.env', postprocess_path=None, timestamp=True) -> str
140+ ```
141+
142+ Parameters:
143+
144+ | Parameter | Description |
145+ | -----------| -------------|
146+ | ` soql_path ` | Path to the ` .soql ` file containing your query. |
147+ | ` output_base_name ` | Base name for the exported CSV (with or without ` .csv ` ). |
148+ | ` output_dir ` | Destination directory (created if it does not exist). Default: current directory. |
149+ | ` env_path ` | Path to the ` .env ` file containing Salesforce credentials. Default: ` .env ` . |
150+ | ` postprocess_path ` | Optional path to a Python script for post-processing. Receives temp_input and final_output CSV paths as argv[ 1] , argv[ 2] . |
151+ | ` timestamp ` | If True (default) prefixes filename like the CLI with a timestamp. Set to False to suppress. |
152+
153+ Returns: Absolute path to the final CSV file.
154+
155+ Example:
156+
157+ ``` python
158+ from soql2csv import export_soql_to_csv
159+
160+ csv_path = export_soql_to_csv(
161+ soql_path = " queries/Accounts.soql" ,
162+ output_base_name = " Accounts.csv" ,
163+ output_dir = " ./exports" ,
164+ env_path = " ./salesforce.env" , # or leave as default '.env'
165+ postprocess_path = None , # or a script like 'scripts/clean_accounts.py'
166+ timestamp = True # set False to disable timestamp prefix
167+ )
168+
169+ print (" CSV exported to" , csv_path)
170+ ```
171+
172+ Minimal example (defaults to ` .env ` in cwd, current directory output, adds timestamp):
173+
174+ ``` python
175+ from soql2csv import export_soql_to_csv
176+ export_soql_to_csv(" query.soql" , " MyData" )
177+ ```
178+
179+ With post-processing (script signature identical to CLI expectations):
180+
181+ ``` python
182+ from soql2csv import export_soql_to_csv
183+ export_soql_to_csv(
184+ soql_path = " query.soql" ,
185+ output_base_name = " FilteredData" ,
186+ postprocess_path = " scripts/filter_script.py"
187+ )
188+ ```
189+
190+ Errors you might encounter:
191+
192+ | Exception | Cause |
193+ | -----------| -------|
194+ | ` FileNotFoundError ` | The ` .soql ` , ` .env ` , or postprocess script path does not exist. |
195+ | ` EnvironmentError ` | Missing required Salesforce environment variables. |
196+ | ` SalesforceAPIError ` | Any Salesforce API interaction failed. |
197+
198+ The function internally mirrors CLI logic: loads env vars, executes Bulk API v2 query, waits for completion, downloads paginated results, optionally runs a postprocess script (on a temporary copy), and returns the final CSV path.
199+
128200## Post-processing
129201
130202Post-processing scripts allow you to transform the exported data. The script automatically receives two arguments:
203+
1312041 . ` sys.argv[1] ` : Path to the input CSV file (exported from Salesforce)
1322052 . ` sys.argv[2] ` : Path to the output CSV file (to write processed data)
133206
@@ -185,23 +258,27 @@ with open(output_csv, 'w', newline='', encoding='utf-8') as outfile:
185258```
186259
187260Run with:
261+
188262``` bash
189263soql2csv query.soql --outfilename=Extract.csv --outfilepath=./output --postprocess=process_script.py
190264```
191265
192266## Examples
193267
194268Basic extraction:
269+
195270``` bash
196271soql2csv queries/Accounts.soql --outfilename=Accounts.csv --outfilepath=./exports
197272```
198273
199274Using a custom environment file:
275+
200276``` bash
201277soql2csv queries/Contacts.soql --outfilename=Contacts.csv --outfilepath=./exports --envpath=./salesforce.env
202278```
203279
204280With post-processing:
281+
205282``` bash
206283soql2csv queries/Opportunities.soql --outfilename=Opportunities.csv --outfilepath=./exports --postprocess=scripts/clean_opps.py
207284```
0 commit comments