0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】GoogleCalendar csv→ics作成

Posted at

csvファイルからicsファイル作成方法

csv記載例
image.png

ソースコード

iCalendar.py
import csv
import datetime
import sys

# =============================================================================
# 設定項目
# =============================================================================
Input_file = '予定.csv'
Output_file = '予定.ics'

# =============================================================================
# 処理部分
# =============================================================================

# csvファイル(カレンダー情報)を読み込む
with open(Input_file, 'r', newline='', encoding='cp932') as csv_file: # encoding='cp932'
    csv_object = csv.reader(csv_file)
    Calendar = [row for row in csv_object] # csv全体を読み込む

# 文字列を iCalendar 形式に整える
for i in range(1, len(Calendar)):
    for j in [1, 3]: # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
        temp01 = Calendar[i][j].split('/') # [Y, m, d]
        temp02 = datetime.date(year = int(temp01[0]), month = int(temp01[1]), day = int(temp01[2])) # int のキャストは01も1になる。
        Calendar[i][j] = temp02.strftime('%Y%m%d')

# 文字列を iCalendar 形式に整える
for i in range(1, len(Calendar)):   # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
    for j in [2, 4]:                # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
        # 時刻が設定されていない場合は"終日"扱いとする。
        if Calendar[i][j] == "":
            break
        else:   
            temp01 = Calendar[i][j].split(':') # [Y, m, d]
            temp02 = datetime.datetime(year = 2000, month = 1, day = 1, hour = int(temp01[0]), minute = int(temp01[1])) # int のキャストは01も1になる。
            Calendar[i][j] = temp02.strftime('T' + '%H%M%S')
            print(Calendar[i][j])

# iCalendar 形式で出力する
with open(Output_file, 'w', newline='\n', encoding='utf-8') as txt_file: # iCalendar は encoding='utf-8' https://www.asahi-net.or.jp/%7ECI5M-NMR/iCal/rfc2445.txt
    txt_file.write('BEGIN:VCALENDAR\n')
    txt_file.write('VERSION:2.0\n')
    txt_file.write('PRODID:-//Test//test-product//ja//\n')
    txt_file.write('CALSCALE:GREGORIAN\n')
    
    for i in range(1, len(Calendar)):
        txt_file.write('BEGIN:VEVENT\n')
        txt_file.write('DTSTART;VALUE=DATE-TIME:' + Calendar[i][1] + Calendar[i][2] + '\n') # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
        txt_file.write('DTEND;VALUE=DATE-TIME:' + Calendar[i][3] + Calendar[i][4] + '\n')   # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
        txt_file.write('SUMMARY:' + Calendar[i][5] + '\n')                                  # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
        txt_file.write('LOCATION:' + Calendar[i][6] + '\n')                                 # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
        txt_file.write('DESCRIPTION:' + Calendar[i][7] + '\n')                              # 0:曜日、1:開始日、2:開始時刻、3:終了日、4:終了時刻、5:タイトル、6:場所、7:説明文
        txt_file.write('END:VEVENT\n')
    txt_file.write('END:VCALENDAR\n')

icsファイルをインポート
image.png

インポート結果
image.png

無題.png
uploading...0

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?