Skip to content

Instantly share code, notes, and snippets.

@Jerakin
Created December 31, 2017 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jerakin/5e1ef84c0ab0515628d77c18ecdf3622 to your computer and use it in GitHub Desktop.
Save Jerakin/5e1ef84c0ab0515628d77c18ecdf3622 to your computer and use it in GitHub Desktop.
Converting Swedbanks exported excel document to a format readable by you need a budget.
"""
Converting Swedbanks exported excel document to a format readable by you need a budget.
"""
import csv
import os
import pandas as pd
bank_file = r"C:\Users\Jerakin\Desktop\Kontohistorik.xls"
you_need_a_budget_file = r"C:\Users\Jerakin\Desktop\ynab.csv"
def convert(input_file, output_file=None):
# Output and temp file location
if not output_file:
output_file = os.path.join(os.path.basename(input_file), "output_ynab.csv")
bank_csv_file = os.path.join(os.path.basename(input_file), "temp_bank.csv")
# Convert our excel to csv
data_xls = pd.read_excel(input_file, 'Blad1', index_col=None)
data_xls.to_csv(bank_csv_file, encoding='utf-8', sep=",", quotechar='"', decimal=",")
with open(output_file, "w", newline="") as output_csv, open(bank_csv_file, "r+", newline="") as input_csv:
csv_reader = csv.reader(input_csv, delimiter=",", quotechar='"')
csv_writer = csv.writer(output_csv, delimiter=",", quotechar='"')
# ynab expected header
header = ["Date", "Payee", "Category", "Memo", "Outflow", "Inflow"]
csv_writer.writerow(header)
# We know that the first 5 rows are garbage + header
next(next(next(next(next(csv_reader)))))
for row in csv_reader:
# Reshuffling of the data from old csv to the new
amount, _date, payee, category, memo, outflow, inflow = row[9], row[5].split("-"), row[7], "", "", "", ""
date = "/".join([_date[2], _date[1], "20{}".format(_date[0])])
if amount.startswith("-"):
outflow = amount[1:]
else:
inflow = amount
write_row = [date, payee, category, memo, outflow, inflow]
csv_writer.writerow(write_row)
# Remove the temp file
if os.path.exists(bank_csv_file):
os.remove(bank_csv_file)
convert(bank_file, you_need_a_budget_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment