Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ nosetests.xml

# Sphinx documentation
docs/_build/

#Generated Files
examples/*/success.csv
examples/*/errors.csv
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ cd examples/

### Create letters from CSV

An example showing how to validate and clean addresses from a CSV spreadsheet full of shipping addresses using Lob's [Address Verification API](https://lob.com/verification/address) and then using the cleaned, valid addresses to dynamically create sample billing letters with variable data using Lob's [Letter API](https://lob.com/services/letters).
An example showing how to dynamically create sample billing letters with variable data using Lob's [Letter API](https://lob.com/services/letters).

In order to run the program enter:

```
cd verify_and_create_letters_from_csv/
cd create_letters_from_csv/
python letter.py input.csv
```

Expand Down
91 changes: 91 additions & 0 deletions examples/create_letters_from_csv/letter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Usage
# python letter.py input.csv
import sys,os
sys.path.insert(0, os.path.abspath(__file__+'../../../..'))

import lob
import csv
import datetime

lob.api_key = 'test_fc26575412e92e22a926bc96c857f375f8b'

skipFirstLine = True

# Column indices in CSV
name = 0
amount = 1
address_line1 = 2
address_line2 = 3
address_city = 4
address_state = 5
address_zip = 6
country = 'US'

try:
sys.argv[1]
except IndexError:
print "Please provide an input CSV file as an argument."
sys.exit()

# Open input files
inputFile = open(sys.argv[1], 'rU')
csvInput = csv.reader(inputFile)

# Create output files
errors = open(os.path.dirname(os.path.abspath(__file__)) + '/errors.csv', 'w')
success = open(os.path.dirname(os.path.abspath(__file__)) + '/success.csv', 'w')

# Loop through input CSV rows
for idx, row in enumerate(csvInput):
if skipFirstLine and idx == 0:
continue

# Create letter for verified addresses
try:
createdLetter = lob.Letter.create(
description = 'Bill for ' + row[name],
metadata = {
'campaign': 'billing_statements',
'csv': inputFile.name
},
to_address = {
'name': row[name],
'address_line1': row[address_line1],
'address_line2': row[address_line2],
'address_city': row[address_city],
'address_zip': row[address_zip],
'address_state': row[address_state],
},
from_address = {
'name': 'Your Name/Company',
'address_line1': '123 Test Avenue',
'address_city': 'San Francisco',
'address_state': 'CA',
'address_zip': '94107',
},
file = open(os.path.dirname(os.path.abspath(__file__)) + '/letter.html', 'r').read(),
data = {
'date': datetime.datetime.now().strftime("%m/%d/%Y"),
'name': row[name],
'amountDue': row[amount]
},
color = True
)
except Exception, e:
print "Error: " + str(e) + " in " + str(row)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errors should also be being logged to errors.csv. Right now we just open the file descriptor and close it. We used to be writing to it when the address verification failed. We want to make sure we log them since they could fail due to rate limits, file dimensions, etc.

else:
outputRow = createdLetter.id + ","
outputRow += createdLetter.url + ","
outputRow += row[name] + ","
outputRow += row[amount] + ","
outputRow += (createdLetter.to_address.address_line1 if createdLetter.to_address.address_line1 != None else " ") + ","
outputRow += (createdLetter.to_address.address_line2 if createdLetter.to_address.address_line2 != None else " ") + ","
outputRow += (createdLetter.to_address.address_city if createdLetter.to_address.address_city != None else " ") + ","
outputRow += (createdLetter.to_address.address_state if createdLetter.to_address.address_state != None else " ") + ","
outputRow += (createdLetter.to_address.address_zip if createdLetter.to_address.address_zip != None else " ") + "\n"
success.write(outputRow)


errors.close()
success.close()
print "\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this print statement?

118 changes: 0 additions & 118 deletions examples/verify_and_create_letters_from_csv/letter.py

This file was deleted.