Python日历模块:HTMLCalendar formatyearpage() 方法
日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。
class calendar.HTMLCalendar(firstweekday=0)
可用于生成 HTML 日历。 formatyearpage() 方法是 HTMLCalendar 实例的方法之一。
Python中的formatyearpage()
方法用于将年份的日历获取为完整的 HTML 页面。
Syntax: formatyearpage(year, width=3, css=’calendar.css’, encoding=None)
Parameter:
year: year of the calendar
width:[Default value is 3] Specifies the width date column
css: [Optional] Name for the CSS to be used.
encoding: [Optional] Specifies the encoding to be used for output
Returns: Return an HTML page for an entire year.
取决于构造函数中指定的或由setfirstweekday()
方法设置的第一个工作日。
代码#1:
# Python program to demonstrate working of formatyearpage() method
# importing calendar module
import calendar
text_cal = calendar.HTMLCalendar(firstweekday = 0)
year = 2018
# Default value of width is 3
# printing formatyearpage
print(text_cal.formatyearpage(year))
输出:
b'
\n
\n
\n
\n
\n
\n
Calendar for 2018 \n
\n
\n
\n
2018
\n
January
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
6
7
\n
8
9
10
11
12
13
14
\n
15
16
17
18
19
20
21
\n
22
23
24
25
26
27
28
\n
29
30
31
\n
\n
\n
February
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
\n
5
6
7
8
9
10
11
\n
12
13
14
15
16
17
18
\n
19
20
21
22
23
24
25
\n
26
27
28
\n
\n
\n
March
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
\n
5
6
7
8
9
10
11
\n
12
13
14
15
16
17
18
\n
19
20
21
22
23
24
25
\n
26
27
28
29
30
31
\n
\n
\n
April
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
\n
2
3
4
5
6
7
8
\n
9
10
11
12
13
14
15
\n
16
17
18
19
20
21
22
\n
23
24
25
26
27
28
29
\n
30
\n
\n
\n
May
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
6
\n
7
8
9
10
11
12
13
\n
14
15
16
17
18
19
20
\n
21
22
23
24
25
26
27
\n
28
29
30
31
\n
\n
\n
June
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
\n
4
5
6
7
8
9
10
\n
11
12
13
14
15
16
17
\n
18
19
20
21
22
23
24
\n
25
26
27
28
29
30
\n
\n
\n
July
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
\n
2
3
4
5
6
7
8
\n
9
10
11
12
13
14
15
\n
16
17
18
19
20
21
22
\n
23
24
25
26
27
28
29
\n
30
31
\n
\n
\n
August
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
\n
6
7
8
9
10
11
12
\n
13
14
15
16
17
18
19
\n
20
21
22
23
24
25
26
\n
27
28
29
30
31
\n
\n
\n
September
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
\n
3
4
5
6
7
8
9
\n
10
11
12
13
14
15
16
\n
17
18
19
20
21
22
23
\n
24
25
26
27
28
29
30
\n
\n
\n
October
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
6
7
\n
8
9
10
11
12
13
14
\n
15
16
17
18
19
20
21
\n
22
23
24
25
26
27
28
\n
29
30
31
\n
\n
\n
November
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
\n
5
6
7
8
9
10
11
\n
12
13
14
15
16
17
18
\n
19
20
21
22
23
24
25
\n
26
27
28
29
30
\n
\n
\n
December
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
\n
3
4
5
6
7
8
9
\n
10
11
12
13
14
15
16
\n
17
18
19
20
21
22
23
\n
24
25
26
27
28
29
30
\n
31
\n
\n
\n
\n'
请注意,可以生成日历的最早年份取决于平台。
代码 #2:宽度为 5
# Python program to demonstrate working of formatyearpage() method
# importing calendar module
import calendar
text_cal = calendar.HTMLCalendar(firstweekday = 0)
# default value of width is 0
# printing formatyearpage
print(text_cal.formatyearpage(2018, 5, encoding = None))
输出:
b'
\n
\n
\n
\n
\n
\n
Calendar for 2018 \n
\n
\n
\n
2018
\n
January
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
6
7
\n
8
9
10
11
12
13
14
\n
15
16
17
18
19
20
21
\n
22
23
24
25
26
27
28
\n
29
30
31
\n
\n
\n
February
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
\n
5
6
7
8
9
10
11
\n
12
13
14
15
16
17
18
\n
19
20
21
22
23
24
25
\n
26
27
28
\n
\n
\n
March
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
\n
5
6
7
8
9
10
11
\n
12
13
14
15
16
17
18
\n
19
20
21
22
23
24
25
\n
26
27
28
29
30
31
\n
\n
\n
April
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
\n
2
3
4
5
6
7
8
\n
9
10
11
12
13
14
15
\n
16
17
18
19
20
21
22
\n
23
24
25
26
27
28
29
\n
30
\n
\n
\n
May
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
6
\n
7
8
9
10
11
12
13
\n
14
15
16
17
18
19
20
\n
21
22
23
24
25
26
27
\n
28
29
30
31
\n
\n
\n
June
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
\n
4
5
6
7
8
9
10
\n
11
12
13
14
15
16
17
\n
18
19
20
21
22
23
24
\n
25
26
27
28
29
30
\n
\n
\n
July
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
\n
2
3
4
5
6
7
8
\n
9
10
11
12
13
14
15
\n
16
17
18
19
20
21
22
\n
23
24
25
26
27
28
29
\n
30
31
\n
\n
\n
August
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
\n
6
7
8
9
10
11
12
\n
13
14
15
16
17
18
19
\n
20
21
22
23
24
25
26
\n
27
28
29
30
31
\n
\n
\n
September
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
\n
3
4
5
6
7
8
9
\n
10
11
12
13
14
15
16
\n
17
18
19
20
21
22
23
\n
24
25
26
27
28
29
30
\n
\n
\n
October
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
5
6
7
\n
8
9
10
11
12
13
14
\n
15
16
17
18
19
20
21
\n
22
23
24
25
26
27
28
\n
29
30
31
\n
\n
\n
November
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
3
4
\n
5
6
7
8
9
10
11
\n
12
13
14
15
16
17
18
\n
19
20
21
22
23
24
25
\n
26
27
28
29
30
\n
\n
\n
December
\n
Mon
Tue
Wed
Thu
Fri
Sat
Sun
\n
1
2
\n
3
4
5
6
7
8
9
\n
10
11
12
13
14
15
16
\n
17
18
19
20
21
22
23
\n
24
25
26
27
28
29
30
\n
31
\n
\n
\n
\n'
HTMLCalendar 有几个属性可以覆盖以自定义日历使用的 CSS 类。属性之一是cssclasses 。
cssclasses是每个工作日使用的 CSS 类的列表。默认列表是cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
。可以每天修改样式,例如 - ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]