-
Notifications
You must be signed in to change notification settings - Fork 3
/
csv-markwhen.py
executable file
·52 lines (42 loc) · 1.68 KB
/
csv-markwhen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""
Process a CSV file and generate formatted output for MarkWhen.
./csv-markwhen.py ~/Downloads/file.csv > ~/Downloads/timeline.mw; npx -i @markwhen/mw ~/Downloads/timeline.mw ~/Downloads/timeline.html; open ~/Downloads/timeline.html
"""
import argparse
import pandas as pd
def process_csv(file_path):
# Read the CSV file
try:
df = pd.read_csv(file_path)
except Exception as e:
print(f"An error occurred while reading the CSV file: {e}")
return
# Check if required columns are present
required_columns = ["Date", "Amount", "Memo"]
if not all(column in df.columns for column in required_columns):
print("The CSV file must contain the columns: Date, Amount, Memo")
return
# Sort the data frame by Date in ascending order
df["Date"] = pd.to_datetime(df["Date"], format="%d/%m/%Y", errors="coerce")
df = df.sort_values(by="Date", ascending=True)
# Remove rows with invalid dates
df = df.dropna(subset=["Date"])
# Output the formatted text
print("---")
print("title: Welcome to Markwhen!")
print("\n#Project1: #d336b1")
print("---")
print("section All Projects")
print("group Project 1 #Project1")
for _, row in df.iterrows():
date = row["Date"].strftime("%Y-%m-%d")
memo = row["Memo"]
amount = row["Amount"]
print(f"{date}: {memo} ({amount})")
print("end-group")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("csv_file", type=str, help="Path to the CSV file")
args = parser.parse_args()
process_csv(args.csv_file)