當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。