📜  Python日历模块:HTMLCalendar formatyear() 方法

📅  最后修改于: 2022-05-13 01:55:15.904000             🧑  作者: Mango

Python日历模块:HTMLCalendar formatyear() 方法

日历模块允许像程序一样输出日历,并提供与日历相关的附加有用功能。 Calendar 模块中定义的函数和类使用理想化的日历,当前的公历在两个方向上无限扩展。

class calendar.HTMLCalendar(firstweekday=0)可用于生成 HTML 日历。 formatyear() 方法是 HTMLCalendar 实例的方法之一。

Python中的formatyear()方法用于将年份的日历获取为 HTML 表格。

取决于构造函数中指定的或由setfirstweekday()方法设置的第一个工作日。

代码#1:

# Python program to demonstrate working of formatyear() method
  
# importing calendar module
import calendar
  
text_cal = calendar.HTMLCalendar(firstweekday = 0)
  
year = 2018
# Default value of width is 3
  
# printing formatyear
print(text_cal.formatyear(year))

输出:

2018
January
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
February
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728    
March
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728293031 
April
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
30      
May
MonTueWedThuFriSatSun
 123456
78910111213
14151617181920
21222324252627
28293031   
June
MonTueWedThuFriSatSun
    123
45678910
11121314151617
18192021222324
252627282930 
July
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
August
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  
September
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
October
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
November
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
2627282930  
December
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
31      

请注意,可以生成日历的最早年份取决于平台。

代码 #2:宽度为 5

# Python program to demonstrate working of formatyear() method
  
# importing calendar module
import calendar
  
text_cal = calendar.HTMLCalendar(firstweekday = 0)
  
  
# default value of width is 0
  
# printing formatyear
print(text_cal.formatyear(2018, 5))

输出:

2018
January
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
February
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728    
March
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728293031 
April
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
30      
May
MonTueWedThuFriSatSun
 123456
78910111213
14151617181920
21222324252627
28293031   
June
MonTueWedThuFriSatSun
    123
45678910
11121314151617
18192021222324
252627282930 
July
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
August
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  
September
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
October
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
November
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
2627282930  
December
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
31