ical2txt.py - implemented start and stop dates to limit the amount of output. If not specified, everything is written.
This commit is contained in:
parent
f9a7cf07f2
commit
01656d6f53
61
ical2txt.py
61
ical2txt.py
@ -6,6 +6,7 @@ from icalendar import Calendar
|
|||||||
import csv
|
import csv
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import warnings
|
import warnings
|
||||||
|
from dateutil.parser import parse
|
||||||
|
|
||||||
warnings.filterwarnings("ignore", category=UserWarning, module='bs4') # We don't want warnings about URL's. We just what the URL printed, if there.
|
warnings.filterwarnings("ignore", category=UserWarning, module='bs4') # We don't want warnings about URL's. We just what the URL printed, if there.
|
||||||
|
|
||||||
@ -89,42 +90,54 @@ def txt_write(icsfile):
|
|||||||
prevdate=""
|
prevdate=""
|
||||||
spent=0
|
spent=0
|
||||||
evcount=0
|
evcount=0
|
||||||
|
evskip=0
|
||||||
|
istart=0
|
||||||
|
istop=4102441200.0 # The year 2100. Hopefully this will not be in use by then ...
|
||||||
|
if sys.argv[2] != '':
|
||||||
|
istart=parse(sys.argv[2]).timestamp()
|
||||||
|
if sys.argv[3] != '':
|
||||||
|
istop=parse(sys.argv[3]).timestamp()
|
||||||
print("Processing events :", end=" ")
|
print("Processing events :", end=" ")
|
||||||
try:
|
try:
|
||||||
with open(txtfile, 'w') as myfile:
|
with open(txtfile, 'w') as myfile:
|
||||||
for event in sortedevents:
|
for event in sortedevents:
|
||||||
|
|
||||||
if prevdate != event.start.strftime("%Y-%m-%d"): # Make a header for each day
|
if prevdate != event.start.strftime("%Y-%m-%d") and spent > 0: # Make a header for each day
|
||||||
if prevdate != '': # If you don't want a summary of the time spent added, comment this section.
|
if prevdate != '': # If you don't want a summary of the time spent added, comment this section.
|
||||||
th=divmod(spent, 3600)[0]
|
th=divmod(spent, 3600)[0]
|
||||||
tm=divmod(spent, 3600)[1]/60
|
tm=divmod(spent, 3600)[1]/60
|
||||||
myfile.write("Time Total: " + '{:02.0f}'.format(th) + ":" + '{:02.0f}'.format(tm) + "\n")
|
myfile.write("\nTime Total: " + '{:02.0f}'.format(th) + ":" + '{:02.0f}'.format(tm) + "\n")
|
||||||
spent=0
|
spent=0
|
||||||
prevdate = event.start.strftime("%Y-%m-%d")
|
if event.start.timestamp() > istart and event.start.timestamp() < istop:
|
||||||
myfile.write("\nMMO (IAM) Worklog, " + prevdate + "\n=============================\n")
|
if prevdate != event.start.strftime("%Y-%m-%d"): # Make a header for each day
|
||||||
|
prevdate = event.start.strftime("%Y-%m-%d")
|
||||||
|
myfile.write("\nWorklog, " + prevdate + "\n===================\n")
|
||||||
|
|
||||||
if event.end == None: print("Event without end. Me no like: " + prevdate + " - " + event.summary.encode('utf-8').decode())
|
duration = event.end - event.start
|
||||||
duration = event.end - event.start
|
ds = duration.total_seconds()
|
||||||
ds = duration.total_seconds()
|
spent += ds
|
||||||
spent += ds
|
hours = divmod(ds, 3600)[0]
|
||||||
hours = divmod(ds, 3600)[0]
|
minutes = divmod(ds,3600)[1]/60
|
||||||
minutes = divmod(ds,3600)[1]/60
|
description=removehtml(event.description.encode('utf-8').decode())
|
||||||
description=removehtml(event.description.encode('utf-8').decode())
|
values = event.start.strftime("%H:%M:%S") + " - " + event.end.strftime("%H:%M:%S") + " (" + '{:02.0f}'.format(hours) + ":" + '{:02.0f}'.format(minutes) + ") " + event.summary.encode('utf-8').decode()
|
||||||
values = event.start.strftime("%H:%M:%S") + " - " + event.end.strftime("%H:%M:%S") + " (" + '{:02.0f}'.format(hours) + ":" + '{:02.0f}'.format(minutes) + ") " + event.summary.encode('utf-8').decode()
|
if event.location != '': values = values + " [" + event.location + "]" # Only include location if there is one
|
||||||
if event.location != '': values = values + " [" + event.location + "]" # Only include location if there is one
|
|
||||||
|
|
||||||
# Remove Google Meet and Skype Meeting part of description
|
# Remove Google Meet and Skype Meeting part of description
|
||||||
trimmed=description.split('-::~')[0].split('......')[0]
|
trimmed=description.split('-::~')[0].split('......')[0]
|
||||||
#print("DescLen: " + str(len(description)) + " TrimmedLen: " + str(len(trimmed)) + " : " + trimmed) # For debugging
|
#print("DescLen: " + str(len(description)) + " TrimmedLen: " + str(len(trimmed)) + " : " + trimmed) # For debugging
|
||||||
description=trimmed
|
description=trimmed
|
||||||
if description != '':
|
if description != '':
|
||||||
values = values + "\n" + description + "\n"
|
values = values + "\n" + description + "\n"
|
||||||
myfile.write(values+"\n")
|
myfile.write(values+"\n")
|
||||||
print("", end=".")
|
print("", end=".")
|
||||||
evcount+=1
|
evcount+=1
|
||||||
print("\n\nWrote " + str(evcount) + " events to ", txtfile, "\n")
|
else:
|
||||||
|
print("", end="S")
|
||||||
|
evskip+=1
|
||||||
|
|
||||||
|
print("\n\nWrote " + str(evcount) + " events to ", txtfile, " and skipped ", str(evskip), " events\n")
|
||||||
except IOError:
|
except IOError:
|
||||||
print("Could not open file! Please close Excel!")
|
print("Could not open file!")
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user