The data exported from the OS X calendar and Google Calendar is in ical format, but it is a bit inconvenient to view it even if it is saved as it is as a past record, so a text file with the date and time and the event name on one line I wanted to keep it. I searched the net and found the icalendar package for Python.
Download the package that handles the ical format in Python from the following site.
https://pypi.python.org/pypi/icalendar
At the time I downloaded it, it was icalendar 3.10 and it supports Python 2.6, 2.7 and 3.3+.
Unzip the .zip file downloaded from the above, move to that folder in the terminal, and install with the following command.
$ python setup.py install
## A program that retrieves events from ical data
The Python program that reads an ical format file and outputs it to standard output is as follows.
The usage is as follows
#### **`$ python iCal2txt.py file | sort > output file`**
```py file | sort > output file
Example of use: `` `$ python iCal2txt.py yotei2016.ical | sort> calendar2016.txt` ``
Now, "calendar2016.txt" is output as text data that extracts the date and time and event name from the data of "yotei2016.ical" and sorts them in order of date and time.
The Python program source is as follows
#### **`iCal2txt.py`**
```python
#!/usr/bin/python
# -*- coding: utf-8 -*-
import codecs
import sys
from icalendar import Calendar, Event
from datetime import datetime
argvs = sys.argv
fin = codecs.open(argvs[1])
cal = Calendar.from_ical(fin.read())
body = ""
for ev in cal.walk():
if ev.name == 'VEVENT':
start_dt = ev.decoded("dtstart")
end_dt = ev.decoded("dtend")
summary = ev['summary'].encode('utf-8')
print "{start} - {end} : {summary}".format(start=start_dt.strftime("%Y/%m/%d %H:%M"), end=end_dt.strftime("%Y/%m/%d %H:%M"), summary=summary)
What time was that event with this file? It is easy to find out.
I used to write a script that retrieves a similar file from an iCal app with AppleScript, but writing it in Python is pretty easy and I like Python. (Because there was a package)
If sorting is not necessary, it is troublesome to specify the output file, so I changed the extension to .txt in the input file name and output it.
ical2txt2.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import codecs
import os
import sys
from icalendar import Calendar, Event
from datetime import datetime
argvs = sys.argv
fin = codecs.open(argvs[1])
cal = Calendar.from_ical(fin.read())
fin.close()
ldata = ""
for ev in cal.walk():
if ev.name == 'VEVENT':
start_dt = ev.decoded("dtstart")
end_dt = ev.decoded("dtend")
summary = ev['summary'].encode('utf-8')
ldata += "{start} - {end} : {summary}".format(start=start_dt.strftime("%Y/%m/%d %H:%M"), end=end_dt.strftime("%Y/%m/%d %H:%M"), summary=summary) + os.linesep
fname,ext = (sys.argv[1]).split(".",1)
fout = fname + ".txt"
f = codecs.open(fout, "w")
f.write(ldata)
f.close()
When sorting in the program, the characters were garbled, so I tried to output before sorting as described above, but I was able to eliminate the garbled characters.
The source is as follows.
Change the line feed code of the output according to your environment.
(Since the behavior of os.linesep
was strange, the line feed code is specified directly.)
Example of use: `$ python ical2txt.py yotei2016.ics`
ical2txt3.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import codecs
import os
import sys
from icalendar import Calendar, Event
from datetime import datetime
argvs = sys.argv
fin = codecs.open(argvs[1])
cal = Calendar.from_ical(fin.read())
fin.close()
li = []
for ev in cal.walk():
if ev.name == 'VEVENT':
start_dt = ev.decoded("dtstart")
end_dt = ev.decoded("dtend")
summary = ev['summary'].encode('utf-8')
li.append("{start} - {end} : {summary}".format(start=start_dt.strftime("%Y/%m/%d %H:%M"), end=end_dt.strftime("%Y/%m/%d %H:%M"), summary=summary).decode('utf-8'))
li.sort()
fname,ext = (sys.argv[1]).split(".",1)
fout = fname + ".txt"
f = codecs.open(fout, "w")
for ld in li:
f.write(ld.encode('utf-8') + "\n")
f.close()
Recommended Posts