マークダウン用のリンク付きカレンダーをプリントするPythonスクリプト
code:script.py
import calendar
import sys
from datetime import datetime
# ---- 年月の決定 ----
if len(sys.argv) == 2:
# 引数あり: YYYY MM
year_str, month_str = sys.argv1.strip().split("/")
year = int(year_str)
month = int(month_str)
else:
# 引数なし: 今日の日付を使用
print("引き数成し")
today = datetime.today()
year = today.year
month = today.month
# ---- カレンダー生成 ----
cal = calendar.Calendar(firstweekday=6) # 日曜始まり
month_days = list(cal.itermonthdays(year, month))
# 曜日ヘッダ
headers = "S", "M", "T", "W", "T", "F", "S"
print("| " + " | ".join(headers) + " |")
print("|" + " --: |"*7)
# 1週間ごとに出力
for i in range(0, len(month_days), 7):
week = month_daysi:i+7
week_cells = []
for day in week:
if day == 0:
week_cells.append(" ")
else:
date_str = f"{year}-{month:02d}-{day:02d}"
week_cells.append(f"{date_str}|{day}")
print("| " + " | ".join(week_cells) + " |")
サンプル
code:bash
markdownCalendar.py 2026/05
@grround-work