Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize a Personalized Calendar with Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how to use Python to achieve a personalized calendar related knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.

I. preparation

Before you begin, make sure that Python and pip are successfully installed on your computer. If not, you can reply to the keyword installation in the official account "Crossin programming classroom" and view the relevant demonstration tutorials. In addition, if you use Python for data analysis, you can install Anaconda directly

Open CMD (start-run-CMD) in Windows environment, and open terminal Terminal (command+ space input Terminal) in Apple system environment, ready to start entering commands to install dependencies.

Enter the following command at the terminal to install the required dependent modules:

Pip install openpyxl

If you see Successfully installed xxx, the installation is successful.

Second, code description 1. Get date

First, to draw a calendar, you need to know how many days there are in each month and what day of the week it is. You can use the calendar package to get this information:

Calendar.monthcalendar (2021, I)

Through this function, we can get the calendar of the I month in 2021, which is similar to a matrix of jambok, so we can iterate through each date like this:

The values obtained by # calendar.monthcalendar are similar to: # [0, 0, 0, 0, 1, 2, 3], # [4, 5, 6, 7, 8, 9, 10], # [11, 12, 13, 14, 15, 16, 17], # [18, 19, 20, 21, 22, 23, 24], # [25, 26, 27, 28, 29, 30, 31] # from left to right, month_calendar = calendar.monthcalendar (2021) I) for row in range (len (month_calendar)): for col in range (len (month_ soldiers [row])): value = month_ soldiers [row] [col]

two。 Draw to get a calendar

The Openpyxl module provides many convenient functions, such as formatting cells, adjusting cell colors, adding pictures, and so on.

Based on Openpyxl, the most convenient way to draw a calendar is to first draw information such as dates into excel, and then extract pictures from excel.

How do I use Openpyxl? Let me give you an example of setting cell fonts:

Sheet.cell (row=j + 4 + count, column=k + 2). Font = Font (u 'Microsoft Yahei, color=text_color, size=14)

Sheet is the corresponding table, row and column are the location of a cell, then set the font property, call the Font class and set the parameters.

If you don't know what parameters the Font class has, you can refer to the openpyxl official documentation.

You can see that the properties of most of the cells are set similar to the above, which is very simple.

3. The work is decorated with a picture every month.

To add a decoration diagram to each month, you need to insert a picture into Excel. Fortunately, Openpyxl provides a convenient way to insert it:

# add picture img = Image (f'12graphs/ {I} .jpg') sheet.add_image (img, 'I2')

12graphs stores 12-month graphs, cycle through each table, and the corresponding charts can be added and rendered to the I2 grid.

You can also DIY yourself, such as changing a picture of a girl friend and giving it to her as a gift.

Please note that the name of the picture can not be wrong, it must be in jpg format from 1 to 12.

4. We also have a mysterious function.

I almost forgot to tell you that our calendar supports notes. Just put in what you want to do one day through set_information () before calling get_month_xlsx to get the document. Such as:

Set_information ('2021-12-1 month,' exam') set_information ('2021-12-1 month,' appointment')

The simple code is explained as above, and the detailed code can be downloaded and viewed in the official account background reply calendar.

Third, run the code

Finally, it's time to run the exciting part of the code. To run this code, you just need to go to the project source folder on your local cmd/terminal and run it:

Python calendary.py

Will automatically generate an excel table called my_calendary.xlsx, this is the calendar we generated.

How to extract a calendar into a picture from it?

Very simple, copy the part you want, paste it into any chat window and turn it into a picture!

These are all the contents of the article "how to use Python to achieve a personalized calendar". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report