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


Python calendar HTMLCalendar formatmonth()用法及代碼示例


日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用函數。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。

class calendar.HTMLCalendar(firstweekday = 0)可用於生成生成HTML日曆。formatmonth()方法是HTMLCalendar實例的方法之一。

Python中的formatmonth()方法用於獲取月份日曆作為HTML表。


用法: formatmonth(year, month, withyear=True)

參數:
year:日曆年
month:日曆月份
withyear:[可選]如果withyear為true,則年份將包含在標題中,否則將僅使用月份名稱。

返回:返回一個月的日曆。

代碼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))

輸出:

<table border="0" cellpadding="0" cellspacing="0" class="month"> 
    <tr> 
        <th colspan="7" class="month">September 2018</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="sun">30</td> 
    </tr> 
</table>

代碼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))

輸出:

<table border="0" cellpadding="0" cellspacing="0" class="month"> 
    <tr> 
        <th colspan="7" class="month">September 2018</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="sun">30</td> 
    </tr> 
</table>


代碼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))

輸出:

<table border ="0" cellpadding ="0" cellspacing ="0" class ="month"> 
    <tr> 
        <th colspan ="7" class ="month">September 2018</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 ="sun">30</td> 
    </tr> 
</table>


相關用法


注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python calendar module | HTMLCalendar formatmonth() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。