{"id":3908,"date":"2020-07-01T18:50:14","date_gmt":"2020-07-01T13:20:14","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3908"},"modified":"2024-06-06T15:50:23","modified_gmt":"2024-06-06T10:20:23","slug":"python-calendar-tutorial-with-example","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-calendar-tutorial-with-example\/","title":{"rendered":"Python CALENDAR Tutorial with Example"},"content":{"rendered":"\n<p>Calendars are very easy to use in <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-oops-concept\/\">Python<\/a>. Calendars are used in many applications like <a href=\"https:\/\/en.wikipedia.org\/wiki\/TradeGecko\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/TradeGecko\" rel=\"nofollow noopener\" target=\"_blank\">inventory management<\/a> systems in which you have to go back and forth between different dates.<\/p>\n\n\n\n<p>Let\u2019s take a look at an example of how to print the January month Calendar in Python.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import calendar\ncalen = calendar.TextCalendar(calendar.SUNDAY)\nstr = calen.formatmonth(2020,1)\nprint(str)<\/pre>\n\n\n\n<p><em>The output will be.<\/em><\/p>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"583\" height=\"340\" src=\"https:\/\/lh3.googleusercontent.com\/bUKCCq5SQCQ7NlbzrxSU34nk009K0QxQeH2lCxvVPYZZ818wcdBLlRZ-tXGD0jYpsIVq-bwRkrudk3KXcbYu91rVuiMcVYczTHda9slst16eXLw_hlyBCkoalIlMooWHN801qs9DSKk0DUkXtg\" alt=\"\" title=\"\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">calen = calendar.TextCalendar(calendar.SUNDAY)<\/pre>\n\n\n\n<p>The calendar.SUNDAY will set the format of the calendar. As you can see above that the calendar starts with Sunday.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">str = calen.formatmonth(2020,1)<\/pre>\n\n\n\n<p>Here \u201c2020\u201d is the year and \u201c1\u201d is the month<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Printing Calendar in HTML<\/h2>\n\n\n\n<p>Developers usually need a calendar in HTML format to make changes in the look and feel. So let\u2019s see how to print the calendar in HTML format.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import calendar\ncalen = calendar.HTMLCalendar(calendar.SUNDAY)\nstr = calen.formatmonth(2020,1)\nprint(str)<\/pre>\n\n\n\n<p><img decoding=\"async\" width=\"624\" height=\"307\" src=\"https:\/\/lh5.googleusercontent.com\/DLJ3pxV2_jRoIJmgjU39e78rrR0vg2P6vx7p1cMJDtSQuuDXWHFEwGmwy1EIeGimKCa4FzIvlW7MB4KSIVYBMLXUeNGpgzxeWv031lV3aHMQmCKwJp6hxs_S0qzHPt1j26sfHTqT0DPVpIpaJg\" alt=\"\" title=\"\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">calen = calendar.HTMLCalendar(calendar.SUNDAY)<\/pre>\n\n\n\n<p>The only thing that changed in the code is the above line in which we first used TextCalendar and now using HTMLCalender.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Loop over the Month<\/h2>\n\n\n\n<p>Sometimes we need to loop over the month days. For that purpose Let\u2019s take a look at the how-to loop over the month of January 2020.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import calendar\ncalen = calendar.TextCalendar(calendar.SUNDAY)\nfor i in calen.itermonthdays(2020,1):\nprint(i)<\/pre>\n\n\n\n<p><img decoding=\"async\" width=\"624\" height=\"335\" src=\"https:\/\/lh6.googleusercontent.com\/Yk230R6gkoeCx5DTOtkUiA6NnRyFja-cnFx0VqxSrolNqAoVIoLeaHN-tnXysgOOkGRmCsuKc7Xn6XL37MpTwbEHTi_0pqAgLFPsUG5YRl5hNFszaFPlS7SWzSuquyIyUNzKZTuhVHqQQJI1uQ\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Using loop we can also print the month name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import calendar\nfor i in calendar.month_name:\nprint(i)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"589\" height=\"591\" src=\"https:\/\/lh3.googleusercontent.com\/hKA_Br0yktaplELuy1j6f8Lzf9gv_hn0Iv4D5cMyYhLcdGFNe6aU_hTI_Q-CTjhIFxiQb2581zXQbd5ZimjADOD3Fi0Tyr5Buh8FXRSPFQeilDz5wlDEcEWEvhry-PaAUpIQ0lQ79z7Y33wUBA\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Let\u2019s print the day names using a loop.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import calendar\nfor i in calendar.day_name:\nprint(i)<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"415\" src=\"https:\/\/lh4.googleusercontent.com\/I4Jh_zFvqKlGMAhEU_dd4wWl_21Aw1u9qF57g9H_svGuRh036Led8-8JCykuur9Z1JS9bdHNJXXYOb_hyWuEo2AZuN7YPjq_3aSIo_d3ACzh4q-6257-MB12l4h821fDbdvRQqafy48DpcLR3w\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"416\" src=\"https:\/\/lh5.googleusercontent.com\/qs3Z_Y_-Tsz7APfkEICvQ2ZnZd-4HrU7xTrPbpPBEB3eulyr2hjGD-PJWG5CcqTFU7qvP5Ykruf3nmX1wQ0wkyEHpOKaCguInHWB2FjTBoWORdw9cAsHge6ywRT4pWGNLoDLt2CJGSbx41zfOA\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calendars are very easy to use in Python. Calendars are used in many applications like inventory management systems in which you have to go back and forth between different dates. Let\u2019s take a look at an example of how to print the January month Calendar in Python. import calendar calen = calendar.TextCalendar(calendar.SUNDAY) str = calen.formatmonth(2020,1) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3924,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[1056,1055,433],"class_list":["post-3908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-calendar-in-html","tag-calendar-tutorial","tag-python"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3908"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3908\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3924"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}