Skip to content

Instantly share code, notes, and snippets.

@debedb
Last active July 16, 2017 05:33
Show Gist options
  • Save debedb/4234cd00e8b21c49e183f933b97dc0e3 to your computer and use it in GitHub Desktop.
Save debedb/4234cd00e8b21c49e183f933b97dc0e3 to your computer and use it in GitHub Desktop.
Quick sort of receipts into folders based on TurboTax clasification
import time
import os
import sys
import shutil
from subprocess import Popen
reader = 'open'
# Inventory, Taxes_and_Licenses, Advertising, Insurance, Interest,
# Rental, Commissions, Contract Labor, Repairs and Maintenance,
# Non-home-office utilities, Other office expenses,
cats = ['Communications',
'Office',
'Charity',
'Advertising',
'Local Travel',
'Business Travel',
'Business Parking',
'Contract Labor',
'Legal and Professional Fees',
'Meals and Entertainment for Employees',
'Meals and Entertainment',
'XX Misc',
'XY Misc',
'Rental Expenses',
'Repairs and Maintenance',
'XX Books',
'XY Supplies',
'XY Supplies',
'Taxes and Licenses',
'Utilities',
'Assets',
'Vehicle',
'XY Training',
'XY Books',
'Professional subscriptions',
'Misc'
]
miles = {}
contents = {}
def input(msg):
sys.stdout.write(msg)
while True:
retval = sys.stdin.readline()
retval = retval.strip()
if retval:
return retval
def dump():
for cat in contents:
f = open('%s.tsv' % cat, 'w')
for line in contents[cat]:
f.write(line)
f.write("\n")
f.flush()
f.close()
def main():
pdfs = os.listdir('queue')
for pdf in pdfs:
if not pdf.endswith('.pdf') and not pdf.endswith('.jpg') and not pdf.endswith('.png'):
print 'Skipping %s' % pdf
continue
fpdf = "queue/%s" % pdf
d = pdf[0:8]
d = d[0:4] + "/" + d[4:6] + "/" + d[6:]
print "0. Skip"
i = 1
for cat in cats:
print "%s. %s" % (i, cat)
i += 1
print "%s. New..." % i
new_choice = i
p = Popen([reader, fpdf])
sys.stdout.write("> ")
while True:
choice = sys.stdin.readline()
choice = choice.strip()
try:
choice = int(choice)
except:
continue
if choice < -1 or choice > len(cats):
continue
break
if choice == 0:
p.kill()
p.wait()
time.sleep(1)
print "Ignoring %s" % pdf
continue
if choice == -1:
p.kill()
p.wait()
time.sleep(1)
print "Removing %s" % fpdf
os.unlink(fpdf)
continue
chosen_cat = None
dir_name = None
if choice == new_choice:
chosen_cat = input("New category")
dir_name = chosen_cat.lower().replace(' ','_')
print "Creating dir %s" % dir_name
os.mkdir(dir_name)
cats.append(chosen_cat)
else:
chosen_cat = cats[choice-1]
dir_name = chosen_cat.lower().replace(' ','_')
if not os.path.exists(dir_name):
print "Creating dir %s" % dir_name
os.mkdir(dir_name)
if chosen_cat not in contents:
contents[chosen_cat] = []
if chosen_cat == 'Ignore':
desc = 'Ignore'
amount = 0
else:
desc = input("Descripion: ")
amount = input("Amount: ")
try:
print "Killing reader: %s" % p.kill()
print "Reader exited with %s" % p.wait()
except Exception,e:
print 'Error killing process %s: %s' % (p, e)
line = "%s\t%s\t%s" % (amount,d,desc)
contents[chosen_cat].append(line)
print "Moving %s to %s" % (fpdf, dir_name)
shutil.copy2(fpdf, dir_name)
try:
time.sleep(1)
os.unlink(fpdf)
except Exception, e:
print e
dump()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment