Pythonのcalendarでカレンダーを取得・出力(テキスト、HTMLなど)
Pythonの標準ライブラリcalendarモジュールで、カレンダーをプレーンテキストやHTML形式の文字列、数値やdatetime.date
などのリストやイテレータとして作成し取得・出力できる。
calendarモジュールには、うるう年を判定したり、月の日数を取得したりする便利な関数も用意されている。
- 関連記事: Pythonでうるう年を判定・カウント・列挙
- 関連記事: Pythonで月の日数・週数を取得
日時を扱う標準ライブラリとしてはdatetimeモジュールもある。
本記事のサンプルコードでは、以下のようにcalendarモジュールをインポートしている。
import calendar
カレンダーをプレーンテキストで取得・出力
月間カレンダー
calendar.month()
で任意の年・月のカレンダーを文字列(str
)で取得できる。
print(calendar.month(2023, 8))
# August 2023
# Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 6
# 7 8 9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31
#
print(type(calendar.month(2023, 8)))
# <class 'str'>
引数w
で列幅、引数l
で行幅を指定可能。
print(calendar.month(2023, 8, w=3, l=2))
# August 2023
#
# Mon Tue Wed Thu Fri Sat Sun
#
# 1 2 3 4 5 6
#
# 7 8 9 10 11 12 13
#
# 14 15 16 17 18 19 20
#
# 21 22 23 24 25 26 27
#
# 28 29 30 31
#
#
calendar.prmonth()
で同様の文字列を標準出力できる。ここでは省略しているが、引数w
, l
を指定可能。
calendar.prmonth(2023, 8)
# August 2023
# Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 6
# 7 8 9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31
年間カレンダー
年間カレンダーはcalendar.calendar()
。引数に年を指定する。
print(calendar.calendar(2023))
# 2023
#
# January February March
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 1 2 3 4 5 1 2 3 4 5
# 2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
# 9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
# 16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
# 23 24 25 26 27 28 29 27 28 27 28 29 30 31
# 30 31
#
# April May June
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 1 2 3 4 5 6 7 1 2 3 4
# 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
# 10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
# 17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
# 24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
#
# July August September
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 1 2 3 4 5 6 1 2 3
# 3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
# 10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
# 17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
# 24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
# 31
#
# October November December
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 1 2 3 4 5 1 2 3
# 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
# 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
# 16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
# 23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
# 30 31
#
print(type(calendar.calendar(2023)))
# <class 'str'>
引数m
で1行に出力する月の数を指定可能。デフォルトは1行に3つの月(3列)。さらに引数c
で月の間のスペースの数を指定できる。以下の例では省略しているが、calendar.month()
と同様に各月内の行幅や列幅を引数w
, l
で指定可能。
print(calendar.calendar(2023, c=3, m=4))
# 2023
#
# January February March April
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 1 2 3 4 5 1 2 3 4 5 1 2
# 2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12 3 4 5 6 7 8 9
# 9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19 10 11 12 13 14 15 16
# 16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26 17 18 19 20 21 22 23
# 23 24 25 26 27 28 29 27 28 27 28 29 30 31 24 25 26 27 28 29 30
# 30 31
#
# May June July August
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 6 7 1 2 3 4 1 2 1 2 3 4 5 6
# 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
# 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
# 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
# 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 31
# 31
#
# September October November December
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 1 1 2 3 4 5 1 2 3
# 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
# 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
# 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
# 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
# 30 31
#
calendar.prcal()
で同様の文字列を標準出力できる。引数w
, l
, c
, m
を指定可能。 ここでは出力結果は省略。
calendar.prcal(2023)
週の始まりの曜日を設定
これまでの例のように、デフォルトは月曜始まり。
calendar.setfirstweekday()
で任意の曜日に変更できる。
calendar.setfirstweekday(calendar.SUNDAY)
print(calendar.month(2023, 8))
# August 2023
# Su Mo Tu We Th Fr Sa
# 1 2 3 4 5
# 6 7 8 9 10 11 12
# 13 14 15 16 17 18 19
# 20 21 22 23 24 25 26
# 27 28 29 30 31
#
calendar.MONDAY
は0
、calendar.SUNDAY
は6
のエイリアス。整数値を指定しても問題ない。現在の設定はcalendar.firstweekday()
で確認できる。
print(calendar.MONDAY)
# 0
print(calendar.SUNDAY)
# 6
calendar.setfirstweekday(0)
print(calendar.month(2023, 8))
# August 2023
# Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 6
# 7 8 9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31
#
print(calendar.firstweekday())
# 0
ロケールを変更して曜日の文字列を変更
曜日を表す文字列はロケールに依存する。
任意のロケールを指定してcalendar.LocaleTextCalendar
オブジェクトを生成し各種メソッドを実行することで、そのロケールに対応した文字列で曜日を出力できる。以下はドイツ語の例。
ltc_de = calendar.LocaleTextCalendar(locale='de_de')
print(ltc_de.formatmonth(2023, 8))
# August 2023
# Mo Di Mi Do Fr Sa So
# 1 2 3 4 5 6
# 7 8 9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31
#
calendar.LocaleTextCalendar
オブジェクトには以下のメソッドがある。月間カレンダーには引数w
, l
を、年間カレンダーにはさらに引数c
, m
を指定できる。
formatmonth()
: 月間カレンダーの文字列を返すprmonth()
: 月間カレンダーを標準出力formatyear()
: 年間カレンダーの文字列を返すpryear()
: 年間カレンダーを標準出力
全角文字の幅は考慮されておらず、日本語の場合はレイアウトが崩れてしまうので注意。
ltc_ja = calendar.LocaleTextCalendar(locale='ja_jp')
print(ltc_ja.formatmonth(2023, 8))
# 8月 2023
# 月 火 水 木 金 土 日
# 1 2 3 4 5 6
# 7 8 9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31
#
カレンダーをHTMLで取得
カレンダーをHTML形式の文字列(str
)として生成し取得できる。テーブル<table>
として表現される。
calendar.HTMLCalendar
オブジェクトを生成し、各種メソッドを実行する。
hc = calendar.HTMLCalendar()
月間カレンダー
月間カレンダーはformatmonth()
。引数withyear
でヘッダーに年を表示するかを指定可能。デフォルトはTrue
。
hc = calendar.HTMLCalendar()
print(hc.formatmonth(2023, 8, withyear=False))
# <table border="0" cellpadding="0" cellspacing="0" class="month">
# <tr><th colspan="7" class="month">August</th></tr>
# <tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr>
# <tr><td class="noday"> </td><td class="tue">1</td><td class="wed">2</td><td class="thu">3</td><td class="fri">4</td><td class="sat">5</td><td class="sun">6</td></tr>
# <tr><td class="mon">7</td><td class="tue">8</td><td class="wed">9</td><td class="thu">10</td><td class="fri">11</td><td class="sat">12</td><td class="sun">13</td></tr>
# <tr><td class="mon">14</td><td class="tue">15</td><td class="wed">16</td><td class="thu">17</td><td class="fri">18</td><td class="sat">19</td><td class="sun">20</td></tr>
# <tr><td class="mon">21</td><td class="tue">22</td><td class="wed">23</td><td class="thu">24</td><td class="fri">25</td><td class="sat">26</td><td class="sun">27</td></tr>
# <tr><td class="mon">28</td><td class="tue">29</td><td class="wed">30</td><td class="thu">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr>
# </table>
#
print(type(hc.formatmonth(2023, 8)))
# <class 'str'>
年間カレンダー
年間カレンダーはformatyear()
。引数width
で1行に表示する月の数を指定可能。デフォルトは3
。
出力結果は省略する。
hc = calendar.HTMLCalendar()
print(hc.formatyear(2023, width=4))
CSSのクラスを設定
これまでの例の出力結果のように、各曜日にCSSのクラスが設定されている。
このクラス名はcssclasses
属性にリストとして格納されている。新たなリストを代入することで変更可能。変更後、formatmonth()
やformatyear()
を実行すると、それらのクラス名を使ったHTMLが取得できる。
hc = calendar.HTMLCalendar()
print(hc.cssclasses)
# ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
hc.cssclasses = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat blue', 'sun red']
Python3.7からは、存在しない日や、月、年に対するCSSクラスの属性も追加された。これも新たな値に変更可能。
print(hc.cssclass_month)
# month
print(hc.cssclass_year)
# year
print(hc.cssclass_noday)
# noday
週の始まりの曜日を設定
週の始まりの曜日はコンストラクタcalendar.HTMLCalendar()
の引数で指定する。月曜が0
で日曜が6
。
hc_sun = calendar.HTMLCalendar(firstweekday=6)
print(hc_sun.formatmonth(2023, 8))
# <table border="0" cellpadding="0" cellspacing="0" class="month">
# <tr><th colspan="7" class="month">August 2023</th></tr>
# <tr><th class="sun">Sun</th><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th></tr>
# <tr><td class="noday"> </td><td class="noday"> </td><td class="tue">1</td><td class="wed">2</td><td class="thu">3</td><td class="fri">4</td><td class="sat">5</td></tr>
# <tr><td class="sun">6</td><td class="mon">7</td><td class="tue">8</td><td class="wed">9</td><td class="thu">10</td><td class="fri">11</td><td class="sat">12</td></tr>
# <tr><td class="sun">13</td><td class="mon">14</td><td class="tue">15</td><td class="wed">16</td><td class="thu">17</td><td class="fri">18</td><td class="sat">19</td></tr>
# <tr><td class="sun">20</td><td class="mon">21</td><td class="tue">22</td><td class="wed">23</td><td class="thu">24</td><td class="fri">25</td><td class="sat">26</td></tr>
# <tr><td class="sun">27</td><td class="mon">28</td><td class="tue">29</td><td class="wed">30</td><td class="thu">31</td><td class="noday"> </td><td class="noday"> </td></tr>
# </table>
#
calendar.setfirstweekday()
は効かないので注意。
ロケールを変更して曜日の文字列を変更
ロケールを変更したい場合は、calendar.LocaleHTMLCalendar
オブジェクトを生成する。メソッドは上述のcalendar.HTMLCalendar
オブジェクトと同じ。
lhc = calendar.LocaleHTMLCalendar(firstweekday=6, locale='ja_jp')
print(lhc.formatmonth(2023, 8))
# <table border="0" cellpadding="0" cellspacing="0" class="month">
# <tr><th colspan="7" class="month">8月 2023</th></tr>
# <tr><th class="sun">日</th><th class="mon">月</th><th class="tue">火</th><th class="wed">水</th><th class="thu">木</th><th class="fri">金</th><th class="sat">土</th></tr>
# <tr><td class="noday"> </td><td class="noday"> </td><td class="tue">1</td><td class="wed">2</td><td class="thu">3</td><td class="fri">4</td><td class="sat">5</td></tr>
# <tr><td class="sun">6</td><td class="mon">7</td><td class="tue">8</td><td class="wed">9</td><td class="thu">10</td><td class="fri">11</td><td class="sat">12</td></tr>
# <tr><td class="sun">13</td><td class="mon">14</td><td class="tue">15</td><td class="wed">16</td><td class="thu">17</td><td class="fri">18</td><td class="sat">19</td></tr>
# <tr><td class="sun">20</td><td class="mon">21</td><td class="tue">22</td><td class="wed">23</td><td class="thu">24</td><td class="fri">25</td><td class="sat">26</td></tr>
# <tr><td class="sun">27</td><td class="mon">28</td><td class="tue">29</td><td class="wed">30</td><td class="thu">31</td><td class="noday"> </td><td class="noday"> </td></tr>
# </table>
#
カレンダーをリストで取得
日付の数値のリスト
calendar.monthcalendar()
で要素が日付の整数となる2次元のリスト(リストのリスト)でカレンダーを取得できる。存在しない日は0
となる。
ここではpprintを使って見やすく出力している。
import calendar
import pprint
pprint.pprint(calendar.monthcalendar(2023, 8))
# [[0, 1, 2, 3, 4, 5, 6],
# [7, 8, 9, 10, 11, 12, 13],
# [14, 15, 16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25, 26, 27],
# [28, 29, 30, 31, 0, 0, 0]]
calendar.setfirstweekday()
で週の始まりの曜日を指定可能。
calendar.setfirstweekday(6)
pprint.pprint(calendar.monthcalendar(2023, 8))
# [[0, 0, 1, 2, 3, 4, 5],
# [6, 7, 8, 9, 10, 11, 12],
# [13, 14, 15, 16, 17, 18, 19],
# [20, 21, 22, 23, 24, 25, 26],
# [27, 28, 29, 30, 31, 0, 0]]
calendar.Calendar
オブジェクトを生成し、monthdayscalendar()
メソッドを呼んでも同様のリストが得られる。この方法の場合、コンストラクタcalendar.Calendar()
の引数firstweekday
で週の始まりの曜日を指定できる。デフォルトはfirstweekday=0
(月曜始まり)。
c = calendar.Calendar(firstweekday=6)
pprint.pprint(c.monthdayscalendar(2023, 8))
# [[0, 0, 1, 2, 3, 4, 5],
# [6, 7, 8, 9, 10, 11, 12],
# [13, 14, 15, 16, 17, 18, 19],
# [20, 21, 22, 23, 24, 25, 26],
# [27, 28, 29, 30, 31, 0, 0]]
c = calendar.Calendar()
pprint.pprint(c.monthdayscalendar(2023, 8))
# [[0, 1, 2, 3, 4, 5, 6],
# [7, 8, 9, 10, 11, 12, 13],
# [14, 15, 16, 17, 18, 19, 20],
# [21, 22, 23, 24, 25, 26, 27],
# [28, 29, 30, 31, 0, 0, 0]]
calendar.Calendar
オブジェクトのyeardayscalendar()
メソッドで、年間カレンダーを取得できる。プレーンテキストやHTMLの場合と同様、各月が縦横に行列状に配置される。引数width
で1行の月の数を指定可能。デフォルトはwidth=3
。
ここではpprintの機能(引数depth
)で省略して表示しているが、[...]
の部分に各月のリストが格納されている。
pprint.pprint(c.yeardayscalendar(2023), depth=2)
# [[[...], [...], [...]],
# [[...], [...], [...]],
# [[...], [...], [...]],
# [[...], [...], [...]]]
pprint.pprint(c.yeardayscalendar(2023, width=4), depth=2)
# [[[...], [...], [...], [...]],
# [[...], [...], [...], [...]],
# [[...], [...], [...], [...]]]
タプルのリスト
calendar.Calendar
のmonthdays2calendar()
で、(日付, 曜日)
のタプルを要素とするリストでカレンダーを取得できる。存在しない日付は0
となる。曜日は月曜が0
で日曜が6
。
c = calendar.Calendar()
pprint.pprint(c.monthdays2calendar(2023, 8))
# [[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)],
# [(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)],
# [(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)],
# [(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)],
# [(28, 0), (29, 1), (30, 2), (31, 3), (0, 4), (0, 5), (0, 6)]]
年間カレンダーはyeardays2calendar()
。例は省略。
datetime.dateのリスト
calendar.Calendar
のmonthdatescalendar()
で、datetime.date
を要素とするリストでカレンダーを取得できる。初週および最終週は前月・次月の値も含む。
c = calendar.Calendar()
pprint.pprint(c.monthdatescalendar(2023, 8))
# [[datetime.date(2023, 7, 31),
# datetime.date(2023, 8, 1),
# datetime.date(2023, 8, 2),
# datetime.date(2023, 8, 3),
# datetime.date(2023, 8, 4),
# datetime.date(2023, 8, 5),
# datetime.date(2023, 8, 6)],
# [datetime.date(2023, 8, 7),
# datetime.date(2023, 8, 8),
# datetime.date(2023, 8, 9),
# datetime.date(2023, 8, 10),
# datetime.date(2023, 8, 11),
# datetime.date(2023, 8, 12),
# datetime.date(2023, 8, 13)],
# [datetime.date(2023, 8, 14),
# datetime.date(2023, 8, 15),
# datetime.date(2023, 8, 16),
# datetime.date(2023, 8, 17),
# datetime.date(2023, 8, 18),
# datetime.date(2023, 8, 19),
# datetime.date(2023, 8, 20)],
# [datetime.date(2023, 8, 21),
# datetime.date(2023, 8, 22),
# datetime.date(2023, 8, 23),
# datetime.date(2023, 8, 24),
# datetime.date(2023, 8, 25),
# datetime.date(2023, 8, 26),
# datetime.date(2023, 8, 27)],
# [datetime.date(2023, 8, 28),
# datetime.date(2023, 8, 29),
# datetime.date(2023, 8, 30),
# datetime.date(2023, 8, 31),
# datetime.date(2023, 9, 1),
# datetime.date(2023, 9, 2),
# datetime.date(2023, 9, 3)]]
datetime.date
は標準ライブラリdatetimeモジュールの型で、年月日を表す。
年間カレンダーはyeardatescalendar()
。例は省略。
カレンダーのイテレータを生成
リストではなくイテレータを生成することもできる。例えばfor
ループで一日ずつ処理する場合はリストを生成するよりも効率的。
calendar.Calendar
オブジェクトを生成し、各メソッドを実行する。コンストラクタcalendar.Calendar()
の引数firstweekday
で週の始まりの曜日を指定できる。デフォルトはfirstweekday=0
(月曜始まり)。
c = calendar.Calendar()
日付の数値のイテレータ
itermonthdays()
は日付の整数値を返すイテレータを生成する。存在しない日は0
となる。最初と最後の5日分を示す。
for d in c.itermonthdays(2023, 8):
print(d)
# 0
# 1
# 2
# 3
# 4
# ...
# 30
# 31
# 0
# 0
# 0
タプルのイテレータ
itermonthdays2()
は(日付, 曜日)
のタプルを返すイテレータを生成する。存在しない日付は0
となる。曜日は月曜が0
で日曜が6
。
for d in c.itermonthdays2(2023, 8):
print(d)
# (0, 0)
# (1, 1)
# (2, 2)
# (3, 3)
# (4, 4)
# ...
# (30, 2)
# (31, 3)
# (0, 4)
# (0, 5)
# (0, 6)
itermonthdays3()
は(年, 月, 日)
のタプルを返すイテレータを生成する。初週および最終週は前月・次月の値も含む。
for d in c.itermonthdays3(2023, 8):
print(d)
# (2023, 7, 31)
# (2023, 8, 1)
# (2023, 8, 2)
# (2023, 8, 3)
# (2023, 8, 4)
# ...
# (2023, 8, 30)
# (2023, 8, 31)
# (2023, 9, 1)
# (2023, 9, 2)
# (2023, 9, 3)
itermonthdays4()
は(年, 月, 日, 曜日)
のタプルを返すイテレータを生成する。初週および最終週は前月・次月の値も含む。曜日は月曜が0
で日曜が6
。
for d in c.itermonthdays4(2023, 8):
print(d)
# (2023, 7, 31, 0)
# (2023, 8, 1, 1)
# (2023, 8, 2, 2)
# (2023, 8, 3, 3)
# (2023, 8, 4, 4)
# ...
# (2023, 8, 30, 2)
# (2023, 8, 31, 3)
# (2023, 9, 1, 4)
# (2023, 9, 2, 5)
# (2023, 9, 3, 6)
datetime.dateのイテレータ
itermonthdates()
はdatetime.date
を返すイテレータを生成する。初週および最終週は前月・次月の値も含む。
for d in c.itermonthdates(2023, 8):
print(d, type(d))
# 2023-07-31 <class 'datetime.date'>
# 2023-08-01 <class 'datetime.date'>
# 2023-08-02 <class 'datetime.date'>
# 2023-08-03 <class 'datetime.date'>
# 2023-08-04 <class 'datetime.date'>
# ...
# 2023-08-30 <class 'datetime.date'>
# 2023-08-31 <class 'datetime.date'>
# 2023-09-01 <class 'datetime.date'>
# 2023-09-02 <class 'datetime.date'>
# 2023-09-03 <class 'datetime.date'>
コマンドラインで利用
calendarモジュールはコマンドラインからも利用できるようになっている。
コマンドラインで、python
コマンド(環境によってはpython3
)に-m
オプションをつけてモジュールとしてcalendarを呼び出す。
python3 -m calendar 2023 8
# August 2023
# Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 6
# 7 8 9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31
各種オプションも使用可能。以下はヘルプの出力。
python3 -m calendar -h
# usage: calendar.py [-h] [-w WIDTH] [-l LINES] [-s SPACING] [-m MONTHS]
# [-c CSS] [-L LOCALE] [-e ENCODING] [-t {text,html}]
# [year] [month]
#
# positional arguments:
# year year number (1-9999)
# month month number (1-12, text only)
#
# options:
# -h, --help show this help message and exit
# -L LOCALE, --locale LOCALE
# locale to be used from month and weekday names
# -e ENCODING, --encoding ENCODING
# encoding to use for output
# -t {text,html}, --type {text,html}
# output type (text or html)
#
# text only arguments:
# -w WIDTH, --width WIDTH
# width of date column (default 2)
# -l LINES, --lines LINES
# number of lines for each week (default 1)
# -s SPACING, --spacing SPACING
# spacing between months (default 6)
# -m MONTHS, --months MONTHS
# months per row (default 3)
#
# html only arguments:
# -c CSS, --css CSS CSS to use for page