-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1f156b
commit 386dde7
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import csv | ||
import argparse | ||
import os | ||
|
||
# Function to convert the CSV format based on the specified criteria | ||
def convert_csv_format(input_file_path): | ||
# Generate output file path by appending "_converted" before the file extension | ||
base = os.path.splitext(input_file_path)[0] | ||
output_file_path = f"{base}_converted.csv" | ||
|
||
with open(input_file_path, mode='r', encoding='utf-8') as infile, \ | ||
open(output_file_path, mode='w', encoding='utf-8', newline='') as outfile: | ||
|
||
reader = csv.DictReader(infile) | ||
fieldnames = ["Date", "Payee", "Memo", "Outflow", "Inflow"] | ||
writer = csv.DictWriter(outfile, fieldnames=fieldnames) | ||
|
||
writer.writeheader() | ||
|
||
for row in reader: | ||
# Using Account number as Memo | ||
memo = row['Account number'] | ||
amount_eur = float(row['Amount (EUR)'].replace('-', '')) if row['Amount (EUR)'] != "-" else 0.0 | ||
# Determining Outflow and Inflow based on Transaction type | ||
if row['Transaction type'] == "Income": | ||
outflow = "" | ||
inflow = amount_eur | ||
else: | ||
outflow = amount_eur | ||
inflow = "" | ||
|
||
new_row = { | ||
"Date": row["Date"], | ||
"Payee": row["Payee"], | ||
"Memo": memo, | ||
"Outflow": outflow, | ||
"Inflow": inflow, | ||
} | ||
writer.writerow(new_row) | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description='Convert a CSV file to a specified format and save it in the same folder with "_converted" appended to the filename.') | ||
parser.add_argument('input_file_path', type=str, help='Path to the input CSV file') | ||
|
||
args = parser.parse_args() | ||
|
||
convert_csv_format(args.input_file_path) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import tkinter as tk | ||
from tkinter import filedialog, messagebox | ||
import csv | ||
import os | ||
|
||
class CSVConverterApp: | ||
def __init__(self, root): | ||
self.root = root | ||
self.root.title('CSV Converter') | ||
self.root.geometry('400x200') | ||
|
||
# Frame for buttons | ||
frame = tk.Frame(self.root) | ||
frame.pack(pady=20) | ||
|
||
# Button to select the input CSV file | ||
self.file_path_var = tk.StringVar() | ||
tk.Button(frame, text="Select CSV File", command=self.select_file).pack(fill=tk.X) | ||
|
||
# Label to display selected file path | ||
self.file_label = tk.Label(frame, textvariable=self.file_path_var, fg="blue") | ||
self.file_label.pack(pady=10) | ||
|
||
# Button to perform conversion | ||
tk.Button(frame, text="Convert File", command=self.convert_file).pack(fill=tk.X) | ||
|
||
def select_file(self): | ||
file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")]) | ||
if file_path: | ||
self.file_path_var.set(file_path) | ||
|
||
def convert_csv_format(self, input_file_path): | ||
base = os.path.splitext(input_file_path)[0] | ||
output_file_path = f"{base}_converted.csv" | ||
|
||
with open(input_file_path, mode='r', encoding='utf-8') as infile, \ | ||
open(output_file_path, mode='w', encoding='utf-8', newline='') as outfile: | ||
|
||
reader = csv.DictReader(infile) | ||
fieldnames = ["Date", "Payee", "Memo", "Outflow", "Inflow"] | ||
writer = csv.DictWriter(outfile, fieldnames=fieldnames) | ||
|
||
writer.writeheader() | ||
|
||
for row in reader: | ||
memo = row['Account number'] | ||
amount_eur = float(row['Amount (EUR)'].replace('-', '')) if row['Amount (EUR)'] != "-" else 0.0 | ||
outflow = amount_eur if row['Transaction type'] != "Income" else "" | ||
inflow = amount_eur if row['Transaction type'] == "Income" else "" | ||
|
||
new_row = { | ||
"Date": row["Date"], | ||
"Payee": row["Payee"], | ||
"Memo": memo, | ||
"Outflow": outflow, | ||
"Inflow": inflow, | ||
} | ||
writer.writerow(new_row) | ||
return output_file_path | ||
|
||
def convert_file(self): | ||
if not self.file_path_var.get(): | ||
messagebox.showerror("Error", "Please select a file first!") | ||
return | ||
|
||
output_file_path = self.convert_csv_format(self.file_path_var.get()) | ||
messagebox.showinfo("Success", f"File converted successfully:\n{output_file_path}") | ||
|
||
if __name__ == "__main__": | ||
root = tk.Tk() | ||
app = CSVConverterApp(root) | ||
root.mainloop() |