当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python HTMLCalendar formatmonth()用法及代码示例


Python HTMLCalendar.formatmonth() 方法

formatmonth() 方法是内置的方法HTMLCalendarcalendarPython 中的模块。它适用于HTMLCalendar类对象并返回表示给定月份日历的 HTML 表格。

模块:

    import calendar

类:

    from calendar import HTMLCalendar

用法:

    formatmonth(year, month, withyear=True)

参数:

  • year:它是一个必需参数,它指定日历的年份
  • month:它是一个必需参数,它指定日历的月份
  • withyear:它是一个可选参数,它指定是否将年份包含在标题中,否则将只使用月份名称;默认值 = True。

返回值:

这个方法的返回类型是<class 'str'>,它将一个月的日历作为 HTML 表格返回。

例:

# Python program to illustrate the 
# use of formatmonth() method

# import class
import calendar

cal = calendar.HTMLCalendar()

year = 2020
month = 2

print(cal.formatmonth(year, month))

输出

<table border="0" cellpadding="0" cellspacing="0" class="month">
    <tr>
        <th colspan="7" class="month">February 2020</th>
    </tr>
    <tr>
        <th class="mon">Mon</th>
        <th class="tue">Tue</th>
        <th class="wed">Wed</th>
        <th class="thu">Thu</th>
        <th class="fri">Fri</th>
        <th class="sat">Sat</th>
        <th class="sun">Sun</th>
    </tr>
    <tr>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="sat">1</td>
        <td class="sun">2</td>
    </tr>
    <tr>
        <td class="mon">3</td>
        <td class="tue">4</td>
        <td class="wed">5</td>
        <td class="thu">6</td>
        <td class="fri">7</td>
        <td class="sat">8</td>
        <td class="sun">9</td>
    </tr>
    <tr>
        <td class="mon">10</td>
        <td class="tue">11</td>
        <td class="wed">12</td>
        <td class="thu">13</td>
        <td class="fri">14</td>
        <td class="sat">15</td>
        <td class="sun">16</td>
    </tr>
    <tr>
        <td class="mon">17</td>
        <td class="tue">18</td>
        <td class="wed">19</td>
        <td class="thu">20</td>
        <td class="fri">21</td>
        <td class="sat">22</td>
        <td class="sun">23</td>
    </tr>
    <tr>
        <td class="mon">24</td>
        <td class="tue">25</td>
        <td class="wed">26</td>
        <td class="thu">27</td>
        <td class="fri">28</td>
        <td class="sat">29</td>
        <td class="noday"> </td>
    </tr>
</table>

例:

# Python program to illustrate the 
# use of formatmonth() method withyear included

# import class
import calendar

cal = calendar.HTMLCalendar()
year = 2010
month = 12
print(cal.formatmonth(year, month, withyear=False))

输出

<table border="0" cellpadding="0" cellspacing="0" class="month">
    <tr>
        <th colspan="7" class="month">December</th>
    </tr>
    <tr>
        <th class="mon">Mon</th>
        <th class="tue">Tue</th>
        <th class="wed">Wed</th>
        <th class="thu">Thu</th>
        <th class="fri">Fri</th>
        <th class="sat">Sat</th>
        <th class="sun">Sun</th>
    </tr>
    <tr>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="wed">1</td>
        <td class="thu">2</td>
        <td class="fri">3</td>
        <td class="sat">4</td>
        <td class="sun">5</td>
    </tr>
    <tr>
        <td class="mon">6</td>
        <td class="tue">7</td>
        <td class="wed">8</td>
        <td class="thu">9</td>
        <td class="fri">10</td>
        <td class="sat">11</td>
        <td class="sun">12</td>
    </tr>
    <tr>
        <td class="mon">13</td>
        <td class="tue">14</td>
        <td class="wed">15</td>
        <td class="thu">16</td>
        <td class="fri">17</td>
        <td class="sat">18</td>
        <td class="sun">19</td>
    </tr>
    <tr>
        <td class="mon">20</td>
        <td class="tue">21</td>
        <td class="wed">22</td>
        <td class="thu">23</td>
        <td class="fri">24</td>
        <td class="sat">25</td>
        <td class="sun">26</td>
    </tr>
    <tr>
        <td class="mon">27</td>
        <td class="tue">28</td>
        <td class="wed">29</td>
        <td class="thu">30</td>
        <td class="fri">31</td>
        <td class="noday"> </td>
        <td class="noday"> </td>
    </tr>
</table>


相关用法


注:本文由纯净天空筛选整理自 Python HTMLCalendar Class | formatmonth() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。