Created
January 11, 2017 17:00
-
-
Save DrDougPhD/6a154881d65aaa6096986d89c1b5c4dc to your computer and use it in GitHub Desktop.
Rename Google Wallet statements to reflect their covered months.
This file contains 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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
SYNOPSIS | |
python rename_google_wallet_statements.py | |
DESCRIPTION | |
Rename Google Wallet statements to reflect their covered months. | |
ASSUMPTIONS | |
* This script is executed within the directory containing | |
Google Wallet statements. | |
* Statement filenames are in their original downloaded format. | |
e.g. 'trs.pdf', 'trs (1).pdf', ... | |
* The oldest statement is named 'trs.pdf'. | |
* The statement named 'trs (1).pdf' is one month after the | |
oldest statement. | |
* For every statement 'trs.pdf (N).pdf', for N >= 1, the | |
statement named 'trs (N+1).pdf' covers the month after | |
'trs (N).pdf'. | |
* This is done automatically if the user downloads | |
their statements from the Google Wallet statement | |
page at https://wallet.google.com/n/statements and | |
downloading the statements, starting from the oldest, | |
and downloading each month after one after another. | |
* The statement named 'trs.pdf' covers January 2014. If not, | |
change STARTING_DATE_STR in this script to equal the first | |
date covered in that statement. | |
ARGUMENTS | |
None currently. | |
AUTHOR | |
Doug McGeehan <djmvfb@mst.edu> | |
LICENSE | |
MIT License | |
Copyright (c) 2017 Doug McGeehan | |
See the bottom of this script for more details. | |
TODO | |
* Instead of relying on numbers within the statements' downloaded | |
filenames, extract the date from within the statement PDF. | |
* Extract transactions from PDF and insert into CSV, possibly even in a | |
format useful for financial software. | |
* Automatically retrieve statements from Google Wallet website. | |
""" | |
import datetime | |
import os | |
import re | |
STARTING_DATE_STR = 'Jan 6, 2014' | |
STARTING_DATE = datetime.datetime.strptime( | |
STARTING_DATE_STR, '%b %d, %Y').date() | |
print(STARTING_DATE) | |
# Filename is like this: 'trs.pdf', 'trs (17).pdf', ... | |
REGEX_PATTERN = re.compile('trs \((\d+)\).pdf') | |
FILENAME_SUFFIX = '{} - Google Wallet Statement.pdf' | |
FILENAME_DATE_FMT = '%Y-%m - %B %Y' | |
def main(): | |
statements = {} | |
for dirpath, dirnames, filenames in os.walk('.'): | |
for f in filenames: | |
if f.endswith('pdf'): | |
fullpath = os.path.join(dirpath, f) | |
months_after_first = get_number_from_filename(fullpath) | |
statements[months_after_first] = (dirpath, f) | |
rename_statements(statements) | |
def get_number_from_filename(path): | |
match = REGEX_PATTERN.search(path) | |
if match: | |
matched_entry = int(match.group(1)) | |
else: | |
matched_entry = 0 | |
return matched_entry | |
def rename_statements(statements): | |
statement_date = STARTING_DATE | |
for m in statements: | |
d, current_filename = statements[m] | |
updated_filename = FILENAME_SUFFIX.format( | |
statement_date.strftime(FILENAME_DATE_FMT) | |
) | |
print('"{0}"\t--->\t"{1}"'.format( | |
current_filename, updated_filename)) | |
os.rename( | |
os.path.join(d, current_filename), | |
os.path.join(d, updated_filename) | |
) | |
statement_date = increment_month(statement_date) | |
def increment_month(current_date): | |
if current_date.month == 12: | |
return current_date.replace(year=current_date.year+1, month=1) | |
else: | |
return current_date.replace(month=current_date.month+1) | |
if __name__ == '__main__': | |
main() | |
""" | |
LICENSE: MIT License | |
Copyright (c) 2017 Doug McGeehan | |
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. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment