code: def generate_ical_file(generator):
      
      global events
      ics_fname = generator.settings['PLUGIN_EVENTS']['ics_fname']
      if not ics_fname:
          return
  
      ics_fname = os.path.join(generator.settings['OUTPUT_PATH'], ics_fname)
      log.debug("Generating calendar at %s with %d events" % (ics_fname, len(events)))
  
      tz = generator.settings.get('TIMEZONE', 'UTC')
      tz = pytz.timezone(tz)
  
      ical = icalendar.Calendar()
      ical.add('prodid', '-//My calendar product//mxm.dk//')
      ical.add('version', '2.0')
  
      DEFAULT_LANG = generator.settings['DEFAULT_LANG']
      curr_events = events if not localized_events else localized_events[DEFAULT_LANG]
  
      for e in curr_events:
          ie = icalendar.Event(
              summary=e.metadata['summary'],
              dtstart=e.dtstart,
              dtend=e.dtend,
              dtstamp=e.metadata['date'],
              priority=5,
              uid=e.metadata['title'] + e.metadata['summary'],
          )
          if 'event-location' in e.metadata:
              ie.add('location', e.metadata['event-location'])
  
          ical.add_component(ie)
  
      with open(ics_fname, 'wb') as f:
          f.write(ical.to_ical())
docs: Generate an iCalendar file