📜  Python日历模块 | HTMLCalendar formatmonth() 方法

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

Python日历模块 | HTMLCalendar formatmonth() 方法

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

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

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

代码#1:

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

输出:


    
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
September 2018
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930

代码#2:带参数宽度

# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.HTMLCalendar(firstweekday = 0)
  
  
# printing formatmonth
print(text_cal.formatmonth(2018, 9))

输出:


    
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
September 2018
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930


代码#3:年份将包含在标题中。

# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.HTMLCalendar(firstweekday = 0)
  
  
# printing formatmonth
print(text_cal.formatmonth(2018, 9, withyear = True))

输出:


    
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
    
        
        
        
        
        
        
        
    
September 2018
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930