-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_maker.py
80 lines (73 loc) · 2.8 KB
/
excel_maker.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import openpyxl.utils.exceptions
from openpyxl import Workbook, load_workbook
from datetime import datetime
from style import *
SHEET_NAME = "Journey"
def make_headlines(work_sheet):
work_sheet["B2"] = "Date"
work_sheet["C2"] = "Title"
work_sheet["D2"] = "Journal"
work_sheet.row_dimensions[2].height = 20
work_sheet.column_dimensions['B'].width = 12
work_sheet.column_dimensions['C'].width = 25
work_sheet.column_dimensions['D'].width = 40
def excel_maker(excel_name, notes_file_path):
while True:
try:
wb = load_workbook(filename=excel_name)
try:
ws = wb[SHEET_NAME]
except KeyError:
wb.create_sheet(SHEET_NAME)
ws = wb[SHEET_NAME]
make_headlines(ws)
break
except openpyxl.utils.exceptions.InvalidFileException:
print("Adding .xlsx at the end")
excel_name = excel_name + ".xlsx"
except FileNotFoundError:
print("Creating new .xlsx file")
wb = Workbook()
ws = wb.active
ws.title = SHEET_NAME
make_headlines(ws)
ws = wb[SHEET_NAME]
break
#---------Counting lines to save last day-----
file = open(notes_file_path, "r", encoding="utf-8")
max_line_count = sum(1 for line in file)
line_count = 0
file.close()
# ------------Opening text file
new_rows_amount = 0
journal_date, journal_title, journal_body = "", "", ""
while True:
try:
file = open(notes_file_path, "r", encoding="utf-8")
for line in file:
line_count += 1
if line.lower().startswith("day") or line_count == max_line_count:
if journal_body != "":
journal_body = journal_body.strip("\n")
data = [None, journal_date, journal_title, journal_body]
ws.append(data)
new_rows_amount += 1
journal_body = ""
if line_count == max_line_count:
break
line = line.removeprefix("Day").strip(" ")
journal_date = datetime.strptime(line[0:10], '%d.%m.%Y').date()
journal_title = line[11:len(line)]
else:
journal_body += line
style(new_rows_amount, ws, wb)
file.close()
wb.save(excel_name)
break
except PermissionError:
print("Can't modify Excel file while is open!")
break
except FileNotFoundError:
print(f"No such file or directory: '{notes_file_path}'")
# notes_file_path = input("Type path to your notes (like '../Journey.txt'):")
print("END")