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

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

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

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

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

Python中的formatyearpage()方法用于将年份的日历获取为完整的 HTML 页面。

取决于构造函数中指定的或由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                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                                \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                                \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
January
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
February
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728    
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
March
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728293031 
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
April
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
30      
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
May
MonTueWedThuFriSatSun
 123456
78910111213
14151617181920
21222324252627
28293031   
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
June
MonTueWedThuFriSatSun
    123
45678910
11121314151617
18192021222324
252627282930 
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
July
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
August
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
September
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
October
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
November
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
2627282930  
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                                \n                     
December
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
31      
\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                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
January
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
February
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728    
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
March
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728293031 
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
April
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
30      
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
May
MonTueWedThuFriSatSun
 123456
78910111213
14151617181920
21222324252627
28293031   
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
June
MonTueWedThuFriSatSun
    123
45678910
11121314151617
18192021222324
252627282930 
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
July
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
August
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
September
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
October
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
November
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
2627282930  
\n                 
                    \n                                                                               \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                                                                                                                                                                                                                                                             \n                     
December
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
31      
\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"]