Skip to content

Instantly share code, notes, and snippets.

@nicop111
Last active March 24, 2025 23:38
Show Gist options
  • Select an option

  • Save nicop111/e99214f0ec082742acb5f0eefcac592a to your computer and use it in GitHub Desktop.

Select an option

Save nicop111/e99214f0ec082742acb5f0eefcac592a to your computer and use it in GitHub Desktop.
google_contacts_to_birthday.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/nicop111/e99214f0ec082742acb5f0eefcac592a/google_contacts_to_birthday.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"This is a Google Colab for extracting birthdays from Google contacts to Google Calendar. A EU privacy law made the automatic syncing of google Contacts to Google Calendar illegal. Follow the instructions:\n",
"\n",
"\n",
"1. Export Google contacts as a **Google csv** from the Desktop website [contacts.google.com](https://contacts.google.com). The file name must be *contacts.csv*\n",
"2. Upload the file on this notebook on the left hand side under files\n",
"3. Run the script below. This will create *birthdays.ics*. Download the file left hand side under files\n",
"4. Import *birthdays.ics* in Google Calendar\n",
"\n"
],
"metadata": {
"id": "SpvA4bj1O4Rp"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uRTtptC3MiID",
"outputId": "aab76117-2466-4667-ce78-2c433ee7eb96"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Calendar file created: birthdays.ics\n"
]
}
],
"source": [
"import csv\n",
"import datetime\n",
"\n",
"def create_birthday_calendar(csv_file, ics_file):\n",
" with open(ics_file, 'w', encoding='utf-8') as f:\n",
" f.write(\"BEGIN:VCALENDAR\\n\")\n",
" f.write(\"VERSION:2.0\\n\")\n",
" f.write(\"PRODID:-//My Birthday Calendar//mxm.dk//\\n\")\n",
"\n",
" with open(csv_file, newline='', encoding='utf-8') as file:\n",
" reader = csv.DictReader(file)\n",
" for row in reader:\n",
" birthday = row.get('Birthday', '').strip()\n",
" if birthday:\n",
" try:\n",
" if birthday.startswith('--'):\n",
" month_day = birthday[2:]\n",
" birth_date = datetime.datetime.strptime(f\"2025-{month_day}\", '%Y-%m-%d').date()\n",
" f.write(\"BEGIN:VEVENT\\n\")\n",
" f.write(f\"SUMMARY:{row.get('First Name', '')} {row.get('Last Name', '')}\\n\")\n",
" f.write(f\"DTSTART:{birth_date.strftime('%Y%m%d')}\\n\")\n",
" f.write(\"RRULE:FREQ=YEARLY;UNTIL=99991231T235959Z\\n\")\n",
" f.write(\"END:VEVENT\\n\")\n",
" else:\n",
" birth_date = datetime.datetime.strptime(birthday, '%Y-%m-%d').date()\n",
" f.write(\"BEGIN:VEVENT\\n\")\n",
" f.write(f\"SUMMARY:{row.get('First Name', '')} {row.get('Last Name', '')}\\n\")\n",
" f.write(f\"DTSTART:{birth_date.strftime('%Y%m%d')}\\n\")\n",
" f.write(\"RRULE:FREQ=YEARLY;UNTIL=99991231T235959Z\\n\")\n",
" f.write(\"END:VEVENT\\n\")\n",
" except ValueError:\n",
" print(f\"Skipping invalid date: {birthday} for {row.get('First Name', '')} {row.get('Last Name', '')}\")\n",
"\n",
" f.write(\"END:VCALENDAR\\n\")\n",
"\n",
" print(f\"Calendar file created: {ics_file}\")\n",
"\n",
"# Example usage\n",
"create_birthday_calendar('contacts.csv', 'birthdays.ics')\n"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment