It’s the annual “painstakingly write out a ton of Christmas cards and mail them” time of the year again, however this year I’ve set out (and succeeded) to make Erin’s life a little easier.

You see, I found this fantastic article that described how one could simply export a Google contacts list to CSV and use the Avery Designer Pro (I’m using the mac version) to map the columns from the export to generate the labels.  This sounded incredibly easy, and so I set out to try to simplify our Christmas card mailing process…

After blindly following the guide (see above), I quickly realized there was a huge issue – Google does not do anything to ensure the resulting CSV file is formatted properly.  While there are many, many columns in the CSV export (including individual fields for the various components of an address), Google has taken the easy way out and simply filled in the “formatted” address column along with all of the line breaks occurring from typing the address in.  The end result was the Avery software printing out 2 (or more) separate labels for every contact, with their address being split across them.  So, I decided to fix the CSV file.

I ended up writing this script in Python, solely because of the built-in CSV library.  I know the new version of Ruby (1.9+) merged the fastercsv gem in, however I just didn’t want to mess with it.  Essentially, the script does two things:

  1. Fixes the “formatted” address column to remove any line breaks
  2. Breaks the address up and puts the parts in to their respective columns

After loading the “fixed” google export file in to the Avery designer, I was able to add the name, address, po box, city, state and zipcode fields to the labels and everything worked like a charm.

At some point, I may get around to turning this in to a a service so one could simply upload the original version and download the fixed, however I just don’t really have an abundance of time and I wouldn’t want anyone freaking out about privacy concerns – so feel free to just download the script and use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
'''
Copyright (c) 2010 Doug McCall [dhm116@dougmccall.com]
 
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
 
from Tkinter import *
 
import csv, tkFileDialog
 
master = Tk()
master.withdraw()
 
# Get the CSV file
filename = tkFileDialog.askopenfilename(filetypes=[('CSV Files', '.csv')])
 
people = open(filename, "rb")
 
rows = people.readlines()
 
# We've got the rows, so close the actual file
people.close()
 
# Remove the header row so it's not processed
header = rows.pop(0);
 
# Start a list of corrected addresses
fixed = []
 
# Iterate through the rows of data
for row in rows:
    # Store this row in something else in case we can't directly manipulate the row data
    data = row
 
    # This should probably be corrected, but for now just arbitrarily pick some length
    # for the contact line to be longer than
    if len(row) > 50:
        #print "Adding new address line"
 
        # If we already have some corrected addresses, append a new line
        # * This probably isn't necessary any more now that I'm feeding the
        #   fixed data as a list to the CSV reader
        if len(fixed) > 0:
            index = len(fixed) - 1
            fixed[index] = ''.join((fixed[index], '\r\n'))
 
        # Add the data to the list of corrected data with any carriage returns removed
        fixed.append(data.rstrip())
 
        #print "\t" + fixed[len(fixed) - 1]
 
    else:
        # We've found a fragmented address line
 
        #print "\tAdding split address line"
 
        # Strip off the carriage return at the end
        data = data.rstrip()
 
        #print "\t\t" + data
 
        # Join this fragmented address line to the previous line of data
        index = len(fixed) - 1
        fixed[index] = ', '.join((fixed[index], data))
 
        #print "\tFixed version"
        #print "\t\t" + fixed[index]
 
        #if data.count(',,,,,,,,,,,,,,,,,,,,,,,') == 0:
        #    print 'Looking for the next full address'
        #    row_start = True
 
# Put the header row back in the corrected data
fixed.insert(0,header)
 
# Load the corrected data in to the CSV reader to more easily manipulate the columns
spamReader = csv.reader(fixed, delimiter=',')
 
# Ask the user where to save the resulting data    
filename = tkFileDialog.asksaveasfilename(filetypes=[('CSV Files', '.csv')])
 
f2 = open(filename, 'w')
 
writer = csv.writer(f2, delimiter=',')
 
rownum = 0
for row in spamReader:
    if rownum > 0:
        # Print out the fully corrected address for debugging
        print str(rownum) + ": " + row[36]
 
        # Isolate the address parts
        addr = row[36].split(',')
 
        # Obtain the State and Zip code from the last part of the address
        state_zip = addr.pop().split()
 
        # If we're not using state abbreviations and there is a space in the state
        # name, we need to merge those back together
        if len(state_zip) > 2:
            state_zip[0] = state_zip[0] + " " + state_zip[1]
 
            # Get rid of the unneeded parts from the merge
            state_zip.pop(1)
 
        # Combine the state and zip back in with the address
        addr.extend(state_zip)
        #print addr
 
        # Set the address column data
        row[37] = addr[0]
 
        # Set the city
        row[38] = addr[-3].lstrip()
 
        # If there is a secondary address (apt. num, etc.)
        if len(addr) > 4:
            row[39] = addr[1]
 
        # Set the state
        row[40] = addr[-2]
 
        # Set the zip code
        row[41] = addr[-1]
 
        #print row
 
    writer.writerow(row)
    rownum += 1
 
f2.close()

This requires python 2.6 or 2.7 (might work on older version, but I haven’t tested).

Just run the script using ‘python fix-addresses.py‘ and it will prompt you for the original csv file and where you would like to save the results.

I’d love to hear back if anyone makes improvements!