Attachment 'csv2eventcal.py'

Download

   1 """
   2     csv2eventcal.py  Version 0.90  May 12, 2006
   3                                                                                                            
   4     This parses to convert outlook .csv file into EventCalendar event data format.
   5                                                                                                            
   6     @copyright: 2006 by Seungik Lee <seungiklee<at>gmail.com>  http://www.silee.net/
   7     @license: GPL
   8     
   9     For more information, please visit http://moinmoin.wikiwikiweb.de/MacroMarket/EventCalendar
  10     
  11     Usage:
  12         $python csv2eventcal.py input_file.csv 
  13         or $python csv2eventcal.py input_file.csv > result.txt
  14         
  15     
  16     .csv file rules:
  17          - date format in the form "YYYY-MM-DD" or modify the 'dateformat' below
  18          - time format in the form "AM HH:MM:SS" or "PM HH:MM:SS" or modify the 'timeformat' below
  19          - the first line which identyfies the csv fields has to be in english
  20          - acceptable fields: "Subject","Start Date","Start Time","End Date","End Time","All day event","Description"
  21 
  22     
  23 """
  24 
  25 # Modify the following lines for your format ###
  26 
  27 # datetime format of outlook .csv file
  28 dateformat = '%Y-%m-%d'
  29 timeformat = '%p %I:%M:%S'
  30 
  31 # datetime format of EventCalendar event data
  32 targetdateformat = '%B %d, %Y'
  33 targettimeformat = '%I:%M %p'
  34 ###
  35 
  36 def formatdatetime(strdatetime, allday=0, flgend=0):
  37     datetimeformat = '%s %s' % (dateformat, timeformat)
  38     EpochSeconds = time.mktime(time.strptime(strdatetime, datetimeformat)) 
  39     now = datetime.datetime.fromtimestamp(EpochSeconds)
  40     if allday:
  41         if flgend:
  42             now = now + datetime.timedelta(days=-1)
  43         datetime_fmt = targetdateformat
  44     else:
  45         datetime_fmt = '%s %s' % (targetdateformat, targettimeformat)
  46     return now.strftime(str(datetime_fmt))
  47 
  48 
  49 ## start main
  50 
  51 import string, os, StringIO, time, datetime
  52 import codecs, sys
  53 
  54 try:
  55     filename = sys.argv[1]
  56 except IndexError:
  57     print 'No file specified.'
  58     sys.exit()
  59 
  60 try:
  61     csvfile = codecs.open(filename, 'r')
  62 except IOError:
  63     print 'Fail to read "%s".' % filename
  64     sys.exit()
  65     
  66 # skip the first line of the fields
  67 in_line = csvfile.readline()
  68 lineno = 1
  69 
  70 while 1:
  71     in_line = csvfile.readline()
  72     lineno += 1
  73     
  74     if in_line == "":
  75         break
  76     
  77     fcount = string.count(in_line, ',')
  78     
  79     try:
  80         if fcount == 5:
  81             [title, startdate, starttime, enddate, endtime, allday] = string.split(in_line, ',')
  82             description = ''
  83         else:
  84             [title, startdate, starttime, enddate, endtime, allday, description] = string.split(in_line, ',')
  85     except ValueError:
  86         print 'PARSE ERROR: line %d syntax error: %s' % (lineno, in_line)
  87         sys.exit()
  88         
  89     title = title[1:-1].replace('""', '"')
  90     startdate = startdate[1:-1]
  91     enddate = enddate[1:-1]
  92     startdate += ' %s' % starttime[1:-1]
  93     enddate += ' %s' % endtime[1:-1]
  94     
  95     if allday[1:-1].startswith('True'):
  96         allday = 1
  97     else:
  98         allday = 0
  99         
 100     total_description = []
 101     
 102     if description:
 103         while 1:
 104             description = description.replace('\n', '')
 105             description = description.replace('\r', '')
 106             total_description.append(description)
 107             if description[-1] != '"':
 108                 description = csvfile.readline()
 109                 lineno += 1
 110                 continue
 111             else:
 112                 break
 113         
 114         total_description = ' '.join(total_description)
 115         total_description = total_description[1:-1]
 116         total_description = total_description.replace('\n', ' ')
 117         total_description = total_description.replace('\r', '')
 118         total_description = total_description.replace('""', '"')
 119         
 120         description = total_description
 121     
 122     try:
 123         startdate = formatdatetime(startdate, allday)
 124     except ValueError:
 125         print 'PARSE ERROR: line %d syntax (datetime format) error: %s' % (lineno, startdate)
 126         sys.exit()
 127     
 128     try:
 129         enddate = formatdatetime(enddate, allday, 1)
 130     except ValueError:
 131         print 'PARSE ERROR: line %d syntax (datetime format) error: %s' % (lineno, startdate)
 132         sys.exit()
 133 
 134     print '== %s ==' % title
 135     print ' start:: %s' % startdate
 136     print ' end:: %s' % enddate
 137 
 138     if description:
 139         print ' description:: %s' % description
 140     print ''
 141 
 142 csvfile.close()
 143 print '----\nCategoryEventCalendar'

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-05-12 15:37:15, 4.5 KB) [[attachment:csv2eventcal.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.