Created
April 3, 2018 20:36
-
-
Save MattSandy/e86f9570f9e423d4bffa363705cb4ffe to your computer and use it in GitHub Desktop.
Chase Statement PDF to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pdfText = require('pdf-text'); | |
var fs = require('fs'); | |
var aggregate = {}; | |
var merchant = ""; | |
var amount = 0; | |
//clear export.csv and write heading | |
fs.writeFile('export.csv', 'Date,Merchant,Amount\n', function(){console.log('export.csv cleared')}); | |
pdfText("statement.pdf", function (err, chunks) { | |
for (var i = 0; i < chunks.length; i++) { | |
//finds the dates | |
if (chunks[i].match(/([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{1,2})/mg) == chunks[i]) { | |
merchant = chunks[i+1]; | |
amount = parseFloat(chunks[i+2]); | |
if(aggregate.hasOwnProperty(merchant)) { | |
//adds amount to existing merchant | |
aggregate[merchant] = aggregate[merchant] + amount; | |
} else { | |
//new merchant | |
aggregate[merchant] = amount; | |
} | |
//line for export.csv | |
var line = '"' + chunks[i] + '","' + merchant + '","' + amount + '"\n'; | |
fs.appendFile('export.csv', line, function (err) { | |
if (err) throw err; | |
}); | |
} | |
} | |
console.log(aggregate); | |
//save aggregate | |
fs.writeFile('aggregate.csv', 'Merchant,Amount\n', function() { | |
console.log('aggregate.csv cleared'); | |
Object.getOwnPropertyNames(aggregate).forEach(function(merchant) { | |
var line = '"' + merchant + '","' + aggregate[merchant] + '"\n'; | |
fs.appendFile('aggregate.csv', line, function (err) { | |
if (err) throw err; | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment