Skip to content

Instantly share code, notes, and snippets.

@tobyspark
Created November 26, 2015 11:01
Show Gist options
  • Save tobyspark/70fb97fce866afd1555b to your computer and use it in GitHub Desktop.
Save tobyspark/70fb97fce866afd1555b to your computer and use it in GitHub Desktop.

Revisions

  1. tobyspark created this gist Nov 26, 2015.
    63 changes: 63 additions & 0 deletions PayPal CSV Exporter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/usr/bin/env python

    import sys
    import csv
    import locale

    if __name__ == '__main__':
    """
    PayPal exports transactions with Gross, Fee, Net in the same line item.
    Accounting software often needs to treat Fee as a separate line item.
    This script parses the PayPal export and separates out these, keeping the running balance true.
    It expects downloads from the following in your PayPal account
    Activity -> Download -> Comma delimited, balance affecting payments
    """

    if (len(sys.argv) < 2):
    sys.exit('Missing input file')

    if (len(sys.argv) < 3):
    sys.exit('Missing output file')

    # needed to parse numbers with comma as thousand separator
    locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

    # read files from arguments
    input_file = sys.argv[1]
    output_file = sys.argv[2]

    csv_in = open(input_file, 'r')
    csv_out = open(output_file, 'w')

    reader = csv.DictReader(csv_in)
    writer = csv.DictWriter(csv_out, ["Date", "Reference", "Paid in and paid out", "Balance"])
    writer.writeheader()

    for items in reader:
    date = items['Date']
    reference = items[' Name'] # There's a space after the first header item, as this line is uniquely separated by comma-space.
    gross = locale.atof(items[' Gross'])
    fee = locale.atof(items[' Fee'])
    balance = locale.atof(items[' Balance'])

    if fee < 0: # fee is -ve
    payment_balance = balance - fee
    writer.writerow({
    "Date": date,
    "Reference": "Fee",
    "Paid in and paid out": fee,
    "Balance": balance,
    })
    writer.writerow({
    "Date": date,
    "Reference": reference,
    "Paid in and paid out": gross,
    "Balance": payment_balance,
    })
    else:
    writer.writerow({
    "Date": date,
    "Reference": reference,
    "Paid in and paid out": gross,
    "Balance": balance,
    })