python日历_如何在Python中动态创建HTML日历

python日历

介绍 ( Introduction )

Python's calendar module is part of the standard library. It allows the output of a calendar by month or by year and also provides other, calendar-related functionality.

Python的 日历模块是标准库的一部分。 它允许按月或按年输出日历,还提供其他与日历相关的功能。

The calendar module itself depends on the datetime module. But we will also need datetime for our own purposes later, so it's best to import both of these. Also, in order to do some string splitting, we will need the re module. Let's import them all in one go.

日历模块本身取决于 日期时间模块。 但是以后我们也需要日期时间来实现我们的目的,因此最好同时导入这两个时间。 另外,为了进行一些字符串拆分,我们将需要re模块。 让我们一次导入它们。

By default, the calendars begin the week with Monday (day 0), per the European convention, and ends with Sunday (day 6). If you prefer Sunday as the first day of the week, use the setfirstweekday() method to change the default to day 6 as follows:

默认情况下,根据欧洲惯例,日历从星期一(第0天)开始,到星期日(第6天)结束。 如果希望将星期日作为一周的第一天,请使用setfirstweekday()方法将默认值更改为第6天,如下所示:

To toggle between the two, you could pass the first day of the week as an argument using the sys module. You would then check the value with an if statement and set the setfirstweekday() method accordingly.

要在两者之间切换,您可以使用sys模块将一周的第一天作为参数传递。 然后,您将使用if语句检查该值,并相应地设置setfirstweekday()方法。

准备一年中的月份 ( Preparing the Months of the Year )

In our ​calendar, it would be nice to have a header for the calendar that reads something like "A Python-Generated Calendar For..." and have the current month and year. In order to do this, we need to get the month and year from the system. This functionality is something that calendar provides, Python can retrieve the month and year. But we still have a problem. As all system dates are numeric and do not contain unabbreviated or non-numeric forms of the months, we need a list of those months. Enter the list year.

在我们的日历中,最好有一个日历标题,它的标题类似于``Python生成的日历...'',并具有当前的月份和年份。 为此,我们需要从系统中获取月份和年份。 日历提供了此功能,Python可以检索月份和年份。 但是我们仍然有一个问题。 由于所有系统日期都是数字形式,并且不包含月份的缩写形式或非数字形式,因此我们需要这些月份的列表。 输入清单年份


year = ['January', 
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']

Now when we get the number of a month, we can access that number (minus one) in the list and get the full month name.

现在,当我们获得一个月的数字时,我们可以在列表中访问该数字(减一)并获得完整的月名称。

称为“今天”的一天 ( A Day Called "Today" )

Starting the main() function, let's ask datetime for the time.

启动main()函数,让我们询问日期时间。

Curiously, the datetime module has a datetime class. It is from this class that we call two objects: now() and date(). The method datetime.datetime.now() returns an object containing the following information: year, month, date, hour, minute, second, and microseconds. Of course, we have no need for the time information. To cull out the date information alone, we pass the results of now() to datetime.datetime.date() as an argument. The result is that today now contains the year, month, and date separated by em-dashes.

奇怪的是, datetime模块有一个datetime类。 我们从这个类中调用了两个对象: now()date()datetime.datetime.now()方法返回一个包含以下信息的对象:年,月,日,时,分,秒和微秒。 当然,我们不需要时间信息。 为了仅剔除日期信息,我们将now()的结果作为参数传递给datetime.datetime.date() 。 结果是, 今天现在包含用短划线分隔的年,月和日。

分割当前日期 ( Splitting the Current Date )

To break this bit of data into more managable pieces, we must split it. We can then assign the parts to the variables current_yr, current_month, and current_day respectively.

要将这些数据分解为更多可管理的部分,我们必须将其拆分。 然后,我们可以将零件分别分配给变量current_yrcurrent_monthcurrent_day

To understand the first line of this code, work from the right to the left and from the inside outward. First, we stringify the object today in order to operate on it as a string. Then, we split it using the em-dash as a delimiter, or token. Finally, we assign those three values as a list to 'current'.

要理解此代码的第一行,请从右到左,从内向外进行操作。 首先,我们今天将对象字符串化,以便将其作为字符串进行操作。 然后,我们使用破折号(em-dash)作为分隔符或标记将其拆分。 最后,我们将这三个值分配为“当前”列表。

In order to deal with these values more distinctly and to call the long name of the current month out of year, we assign the number of the month to current_no. We can then do a bit of subtraction in the subscript of year and assign the month name to current_month.

为了更清楚地处理这些值并在一年中调用当前月份的长名称,我们将月份号分配给current_no 。 然后,我们可以对year的下标进行一些减法,然后将month名称分配给current_month

In the next line, a bit of substitution is needed. The date which is returned from datetime is a two-digit value even for the first nine days of the month. A zero functions as a place holder, but we would rather our calendar have just the single digit. So we substitute no value for every zero that begins a string (hence '\A'). Finally, we assign the year to current_yr, converting it to an integer along the way.

在下一行中,需要一些替换。 从datetime返回的日期即使是一个月的前九天也是两位数的值。 零用作占位符,但我们希望日历只包含一位数字。 因此,我们不会将以字符串开头的每个零替换为任何值(因此为'\ A')。 最后,我们将year分配给current_yr ,然后将其转换为整数。

Methods that we will call later will require input in integer format. Therefore, it is important to ensure that all of the date data is saved in integer, not string, form.

我们稍后将调用的方法将需要以整数格式输入。 因此,重要的是要确保所有日期数据都以整数而不是字符串形式保存。

HTML和CSS序言 ( The HTML and CSS Preamble )

Before we print the calendar, we need to print the​ ​HTML preamble and CSS layout for our calendar. Go to this page for the code to print the CSS and HTML preamble for the calendar. and copy the code into your program file. The CSS in the HTML of this file follows the template offered by Jennifer Kyrnin, About's Guide to Web Design. If you do not understand this part of the code, you may want to consult her helps for learning CSS and HTML. Finally, to customize the month name, we need the following line:

之前我们打印日历,我们需要打印的HTML我们的日历序言和CSS布局。 转到该页面以获取用于打印日历CSS和HTML序言的代码。 并将代码复制到您的程序文件中。 该文件HTML中CSS遵循Jennifer Kyrnin提供的模板,该模板为About的Web设计指南。 如果您不理解代码的这一部分,则可能需要咨询她的有关学习CSS和HTML的帮助。 最后,要自定义月份名称,我们需要以下行:


print '

打印星期几 ( Printing the Days of the Week )

Now that the basic layout is output, we can set up the calendar itself. A calendar, at its most basic point, is a table. So let's make a table in our HTML:

现在已经输出了基本布局,我们可以设置日历本身了。 日历从最基本的角度来说就是一张桌子。 因此,让我们在HTML中创建一个表格:

获取日历数据 ( Getting the Calendar Data )

Now we need to create the actual calendar. To get the actual calendar data, we need the calendar module's monthcalendar() method. This method takes two arguments: the year and the month of the desired calendar (both in integer form). It returns a list which contains lists of the dates of the month by week. So if we count the number of items in the returned value, we have the number of weeks in the given month.

现在我们需要创建实际的日历。 要获取实际的日历数据,我们需要日历模块的monthcalendar()方法。 此方法有两个参数:所需日历的年和月(均为整数形式)。 它返回一个列表,其中包含每月的日期列表。 因此,如果我们计算返回值中的项目数,则可以得到给定月份中的周数。

一个月中的周数 ( The Number of Weeks In A Month )

Knowing the number of weeks in the month, we can create a for loop which counts through a range() from 0 to the number of weeks. As it does, it will print out the rest of the calendar.

知道一个月中的星期数,我们可以创建一个for循环,该循环通过从0到星期数的range()进行计数。 这样,它将打印出日历的其余部分。

We will discuss this code line-by-line on the next page.

我们将在下一页逐行讨论此代码。

检查“ for”循环 ( The 'for' Loop Examined )

After this range has been started, the dates of the week are culled from month according to the value of the counter and assigned to week. Then, a tabular row is created to hold the calendar dates.

开始此范围后,将根据计数器的值从月份中删除星期几,并将其分配给week 。 然后,创建一个表格行来保存日历日期。

A for loop then walks through the days of the week so they can be analyzed. The calendar module prints a '0' for every date in the table that does not have a valid value. A blank value would work better for our purposes so we print the bookends of tabular data without a value for those dates.

然后, for循环遍历一周中的每一天,以便可以对其进行分析。 日历模块为表中没有有效值的每个日期打印一个“ 0”。 空值会更好地达到我们的目的,因此我们在打印表格数据的书挡时没有这些日期的值。

Next, if the day is the current one, we should highlight it somehow. Based on the td class today, the CSS of this page will cause the current date to be rendered against a dark background instead of the light background of the other dates.

接下来,如果今天是当前日期,则应以某种方式突出显示它。 根据今天td类,此页面CSS将使当前日期呈现为深色背景,而不是其他日期的浅色背景。

Finally, if the date is a valid value and is not the current date, it is printed as tabular data. The exact color combinations for these are held in the CSS style preamble.

最后,如果日期是有效值而不是当前日期,则将其打印为表格数据。 这些样式的确切颜色组合保存在CSS样式序言中。

The last line of the first for loop closes the row. With the calendar printed our task is finished and we can close the HTML document.

第一个for循环的最后一行关闭该行。 完成日历打印后,我们的任务完成了,我们可以关闭HTML文档。

调用main()函数 ( Calling the main() Function )

As all of this code is in the main() function, do not forget to call it.

由于所有这些代码都在main()函数中,因此请不要忘记调用它。

Just this simple calendar can be used in any way that needs a calendar representation. By hyperlinking the dates in the HTML, one can easily create a diary functionality. Alternatively, one can check against a diary file and then reflect which dates are taken by their color. Or, if one converts this program into a CGI script, one can have it generated on the fly.

只是此简单的日历可以以任何需要日历表示的方式使用。 通过超链接HTML中的日期,可以轻松创建日记功能。 或者,可以检查日记文件,然后根据其颜色反映出哪个日期。 或者,如果有人将此程序转换为CGI脚本,则可以即时生成它。

Of course, this is just an overview of the calendar module's functionality. The documentation gives a fuller view.

当然,这只是日历模块功能的概述。 该文档提供了更完整的视图。

翻译自: https://www.thoughtco.com/create-a-html-calendar-2813508

python日历

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值