47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
|
# Script de conversion de fichiers calendriers au format CalDAV (.ics) vers un tableur (.csv)
|
||
|
# Auteur : Elias Martin - elias@hereli.fr
|
||
|
# Licence : EUPL V1.2
|
||
|
|
||
|
## Credits : https://stackoverflow.com/questions/59958941/calendar-ics-to-csv-in-python
|
||
|
## and https://stackoverflow.com/questions/18262293/how-to-open-every-file-in-a-folder
|
||
|
## and https://stackoverflow.com/questions/9319317/quick-and-easy-file-dialog-in-python
|
||
|
|
||
|
import vobject
|
||
|
import csv
|
||
|
import os, glob
|
||
|
#from tkinter.filedialog import askopenfilename
|
||
|
#filename = askopenfilename()
|
||
|
|
||
|
# Changer cette variable pour écrire dans un nouveau fichier
|
||
|
nom_fichier_sortie = "calendriers_mediateurs.csv"
|
||
|
|
||
|
with open(nom_fichier_sortie, mode='w') as csv_out:
|
||
|
|
||
|
csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
|
||
|
|
||
|
# Changer ces éléments pour renommer les colonnes du fichier de sortie
|
||
|
csv_writer.writerow(['Evt', 'Début', 'Fin', 'Description', 'Lieu'])
|
||
|
|
||
|
# Décommenter (et indenter la fin du script) pour activer
|
||
|
for filename in glob.glob('*.ics'):
|
||
|
# read the data from the file
|
||
|
data = open(os.path.join(os.getcwd(), filename)).read()
|
||
|
|
||
|
#Commenter pour ne pas activer la fenêtre de sélection d'un seul fichier
|
||
|
# data = open(filename).read()
|
||
|
|
||
|
# iterate through the contents
|
||
|
for cal in vobject.readComponents(data):
|
||
|
for component in cal.components():
|
||
|
if component.name == "VEVENT":
|
||
|
writerow = []
|
||
|
for attr in ['summary', 'dtstart', 'dtend', 'description', 'location']:
|
||
|
if hasattr(component, attr):
|
||
|
writerow.append(getattr(component, attr).valueRepr())
|
||
|
else:
|
||
|
writerow.append('-')
|
||
|
|
||
|
#print(writerow)
|
||
|
csv_writer.writerow(writerow)
|
||
|
|