|
|
|
@@ -26,11 +26,11 @@ def parse_norwegian_date(date_str): |
|
|
|
if pd.isna(date_str) or date_str == '': |
|
|
|
return '' |
|
|
|
try: |
|
|
|
# Parse DD.MM.YYYY and convert to YYYY-MM-DD |
|
|
|
date_obj = pd.to_datetime(date_str, format='%d.%m.%Y') |
|
|
|
return date_obj.strftime('%Y-%m-%d') |
|
|
|
# Parse DD.MM.YYYY and convert to date object |
|
|
|
return pd.to_datetime(date_str, format='%d.%m.%Y') |
|
|
|
except (ValueError, TypeError): |
|
|
|
return str(date_str) # Return original if parsing fails |
|
|
|
print(f"Invalid date format: {date_str}") |
|
|
|
exit(1) |
|
|
|
|
|
|
|
def parse_bank_sparebank1(data): |
|
|
|
""" |
|
|
|
@@ -85,11 +85,13 @@ def parse_bank_norwegian(data): |
|
|
|
BANKS = { |
|
|
|
"Sparebank1": { |
|
|
|
"patterns": ["OversiktKonti*.csv"], |
|
|
|
"output_filename": "YNAB-{bank}-FROM-{first_date}-TO-{last_date}", |
|
|
|
"parse_function": parse_bank_sparebank1, |
|
|
|
"delimiter": ";" |
|
|
|
}, |
|
|
|
"Norwegian": { |
|
|
|
"patterns": ["BankNorwegian*.xlsx", "Statement*.xlsx"], |
|
|
|
"output_filename": "YNAB-{bank}-FROM-{first_date}-TO-{last_date}", |
|
|
|
"parse_function": parse_bank_norwegian |
|
|
|
} |
|
|
|
# Add more banks and patterns as needed |
|
|
|
@@ -205,12 +207,26 @@ def convert_bank_statements_to_ynab(input_files=None): |
|
|
|
print(f"No data processed for {file_path}") |
|
|
|
continue |
|
|
|
|
|
|
|
# Define output file with "ynab-" prefix |
|
|
|
output_filename = f"ynab-{file_path.name}" |
|
|
|
if not output_filename.endswith('.csv'): |
|
|
|
output_filename = Path(output_filename).stem + '.csv' |
|
|
|
|
|
|
|
output_file = output_directory / output_filename |
|
|
|
filename_placeholders = { |
|
|
|
'bank': bank_name, |
|
|
|
'first_date': ynab_data['Date'].min().date(), |
|
|
|
'last_date': ynab_data['Date'].max().date(), |
|
|
|
} |
|
|
|
|
|
|
|
file_retry_count = 0 |
|
|
|
while True: |
|
|
|
output_filename = bank_config["output_filename"].format(**filename_placeholders) |
|
|
|
|
|
|
|
if file_retry_count > 0: |
|
|
|
output_filename += f" ({file_retry_count})" |
|
|
|
|
|
|
|
output_filename += ".csv" |
|
|
|
output_file = output_directory / output_filename |
|
|
|
|
|
|
|
if not output_file.exists(): |
|
|
|
break |
|
|
|
|
|
|
|
file_retry_count += 1 |
|
|
|
|
|
|
|
# Export to CSV for YNAB import |
|
|
|
ynab_data.to_csv(output_file, index=False) |
|
|
|
|