Created
April 11, 2021 04:59
-
-
Save davidair/3aebc436dd4847bc265844ceba999505 to your computer and use it in GitHub Desktop.
Filename renamer for TD Canada Trust statements to normalize timestamps
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
# Copyright 2021 Google LLC. | |
# SPDX-License-Identifier: Apache-2.0 | |
import os | |
import pathlib | |
import re | |
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] | |
pattern_td = r'(.+)([A-Za-z]{3})_(\d\d)-([A-Za-z]{3})_(\d\d)\_(\d\d\d\d).pdf' | |
pattern_loc = r'(.+)([A-Za-z]{3})_(\d\d)(_|-)(\d\d\d\d).pdf' | |
working_directory = pathlib.Path.home().joinpath('Downloads', 'statements') | |
def HandleLOCStatement(filepath): | |
m = re.match(pattern_loc, filepath.name) | |
if m: | |
year = int(m.group(5)) | |
return '%s%d-%02d-%s.pdf' % (m.group(1), year, months.index(m.group(2)) + 1, m.group(3)) | |
return None | |
def HandleTDStatement(filepath): | |
m = re.match(pattern_td, filepath.name) | |
if m: | |
year_from = int(m.group(6)) | |
year_to = year_from | |
if m.group(2) == 'Dec' and m.group(4) == 'Jan': | |
year_from = year_from - 1 | |
return '%s%d-%02d-%s_%d-%02d-%s.pdf' % (m.group(1), year_from, months.index(m.group(2)) + 1, m.group(3), year_to, months.index(m.group(4)) + 1, m.group(5)) | |
return None | |
print('Starting renamer...') | |
for filepath in pathlib.Path.iterdir(working_directory): | |
print('Checking %s...' % filepath.name) | |
renamed = HandleTDStatement(filepath) | |
if renamed: | |
print('%s -> %s' % (str(filepath), renamed)) | |
filepath.rename(filepath.parent.joinpath(renamed)) | |
renamed = HandleLOCStatement(filepath) | |
if renamed: | |
print('%s -> %s' % (str(filepath), renamed)) | |
filepath.rename(filepath.parent.joinpath(renamed)) | |
print('All done!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment