calendar
是Python标准库中的一个模块,它提供了一组用于打印日历的函数。使用这个库,你可以轻松地生成各种格式和样式的日历。
1. prcal(year)
参数w,c,l是打印间隔,w是日期之间的间隔;c是月份之间的间隔;l是行与行的间隔。
直接打印:
import calendar
calendar.prcal(2024)
2024
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 2 3 4 5 6 7 1 2 3 4 1 2 3
8 9 10 11 12 13 14 5 6 7 8 9 10 11 4 5 6 7 8 9 10
15 16 17 18 19 20 21 12 13 14 15 16 17 18 11 12 13 14 15 16 17
22 23 24 25 26 27 28 19 20 21 22 23 24 25 18 19 20 21 22 23 24
29 30 31 26 27 28 29 25 26 27 28 29 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 3 4 5 6 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 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 3 4 5 6 7 1 2 3 4 1
8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8
15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15
22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22
29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29
30
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 2 3 4 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
默认并排打印3个月份,参数m=2设置成2个月并排:
calendar.prcal(2024, m=2)
2024
January February
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
8 9 10 11 12 13 14 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25
29 30 31 26 27 28 29
March April
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30
May June
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 29 30
July August
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
8 9 10 11 12 13 14 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25
29 30 31 26 27 28 29 30 31
September October
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 6
2 3 4 5 6 7 8 7 8 9 10 11 12 13
9 10 11 12 13 14 15 14 15 16 17 18 19 20
16 17 18 19 20 21 22 21 22 23 24 25 26 27
23 24 25 26 27 28 29 28 29 30 31
30
November December
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 1
4 5 6 7 8 9 10 2 3 4 5 6 7 8
11 12 13 14 15 16 17 9 10 11 12 13 14 15
18 19 20 21 22 23 24 16 17 18 19 20 21 22
25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
2. calendar(year)
参数与prcal()完全一致,只是它返回的是字符串,打印结果 print(calendar.calendar(2024)) 与calendar.prcal(2024) 一样效果:
>>> calendar.calendar(2024)
‘ 2024\n\n January February March\nMo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n 1 2 3 4 5 6 7 1 2 3 4 1 2 3\n 8 9 10 11 12 13 14 5 6 7 8 9 10 11 4 5 6 7 8 9 10\n15 16 17 18 19 20 21 12 13 14 15 16 17 18 11 12 13 14 15 16 17\n22 23 24 25 26 27 28 19 20 21 22 23 24 25 18 19 20 21 22 23 24\n29 30 31 26 27 28 29 25 26 27 28 29 30 31\n\n April May June\nMo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n 1 2 3 4 5 6 7 1 2 3 4 5 1 2\n 8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9\n15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16\n22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23\n29 30 27 28 29 30 31 24 25 26 27 28 29 30\n\n July August September\nMo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n 1 2 3 4 5 6 7 1 2 3 4 1\n 8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8\n15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15\n22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22\n29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29\n 30\n\n October November December\nMo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su\n 1 2 3 4 5 6 1 2 3 1\n 7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8\n14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15\n21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22\n28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29\n 30 31\n’
3.prmonth(year, month)
打印单个月份。
>>> calendar.prmonth(2024,3)
March 2024
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
4.month(year, month)
返回单月的日历字符串。
>>> print(calendar.month(2024,3))
March 2024
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
5. monthrange(year, month)
返回该月第一天的星期和当月总天数。
>>> calendar.monthrange(2024,2)
(calendar.THURSDAY, 29)
>>> calendar.monthrange(2024,3)
(calendar.FRIDAY, 31)
>>> calendar.monthrange(2024,4)
(calendar.MONDAY, 30)
6. weekday(year, month, day)
返回指定日期的星期数
>>> calendar.weekday(2024, 3, 16)
calendar.SATURDAY
>>> calendar.weekday(2025, 1, 1)
calendar.WEDNESDAY
7. month_name
月份名列表,索引是从1开始的,索引0为空串。
>>> [calendar.month_name[i] for i in range(13)]
[”, ‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’]
8. day_name
星期名列表,索引是从0开始的。
>>> [calendar.day_name[i] for i in range(7)]
[‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’]
9.isleap(year)
判断年份是否闰年。
>>> calendar.isleap(2024)
True
>>> calendar.isleap(2025)
False
10. weekheader(width)
日历的表格头。
>>> calendar.weekheader(1)
‘M T W T F S S’
>>> calendar.weekheader(2)
‘Mo Tu We Th Fr Sa Su’
>>> calendar.weekheader(3)
‘Mon Tue Wed Thu Fri Sat Sun’
>>> calendar.weekheader(4)
‘Mon Tue Wed Thu Fri Sat Sun ‘
11.Day()
一星期七天的名称枚举。
>>> calendar.Day
<enum ‘Day’>
>>> [calendar.Day(i) for i in range(7)]
[calendar.MONDAY, calendar.TUESDAY, calendar.WEDNESDAY, calendar.THURSDAY, calendar.FRIDAY, calendar.SATURDAY, calendar.SUNDAY]
12.setfirstweekday(0)
星期的第一天显示的设置,默认值是0即星期一为第一天。
>>> import calendar
>>> calendar.prmonth(2024,3)
March 2024
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.setfirstweekday(6)
>>> calendar.prmonth(2024,3)
March 2024
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.setfirstweekday(3)
>>> calendar.prmonth(2024,3)
March 2024
Th Fr Sa Su Mo Tu We
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.setfirstweekday(0)
>>> calendar.prmonth(2024,3)
March 2024
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
完
版权声明:本文为博主作者:Hann Yang原创文章,版权归属原作者,如果侵权,请联系我们删除!
原文链接:https://blog.csdn.net/boysoft2002/article/details/136770420