Guide language:

The New Year is coming, and many people will buy a calendar. There are many calendars with different themes and styles, but have you ever thought about creating your own personalized electronic calendar?

This tutorial will teach you how to create a calendar in Python.

A, preparation,

Before you start, make sure Python and PIP are successfully installed on your computer. If not, you can reply to the keyword installation in the public account “Crossin programming classroom” and check the relevant demonstration tutorial. In addition, if you are using Python for data analysis, you can install Anaconda directly. See: I used to get frustrated installing libraries until I met Anaconda.

In Windows, open CMD (Start – Run – CMD). In Apple, open Terminal (command+ space enter Terminal).

Enter the following command on the terminal to install the required dependency modules:

pip install openpyxl
Copy the code

If Successfully installed XXX is displayed, the installation is successful.

Ii. Code description

1. Obtain the date

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

calendar.monthcalendar(2021, i)
Copy the code

Using this function, we can get the calendar of I months in 2021, which is like a J * K matrix, so we can iterate over each date like this:

# calendar. monthCalendar gets a value like this: # [[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 for Sunday to Saturday i) for row in range(len(month_calendar)): for col in range(len(month_calendar[row])): value = month_calendar[row][col]Copy the code

2. Draw the calendar

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

Based on Openpyxl, the easiest way to draw a calendar is to draw information like dates into Excel and then extract images from Excel.

How to use Openpyxl? To give you an example of setting cell fonts:

Sheet. Cell (row=j + 4 + count, column=k + 2). Font = font (u' m ', color=text_color, size=14)Copy the code

Sheet is the corresponding table, row and column are the positions of certain cells, and then you set the font property, call the FONT class, and set the parameters.

If you don’t know what the Font type parameter, you can refer to openpyxl official document: openpyxl. Readthedocs. IO/en/stable /

You can see that most of the cell properties are set as above, very simple.

3. The work is decorated with a picture of each month

To add a decorative image for each month, you need to insert an image into Excel. Fortunately, Openpyxl provides a convenient way to insert an image:

Img = Image(f’12graphs/{I}.jpg’) sheet.add_image(img, ‘I2’)

12Graphs contains 12 months of graphs. By iterating over each table, the corresponding graphs can be added and rendered to the I2 grid.

You can also make your own, like changing a picture of your girlfriend and giving it to her as a gift

Please note that the name of the image must be correct and must be JPG from 1 to 12.

4. We also have a mystery feature

Before calling get_month_xlsx to get the document, use set_information() to put in what you want to do that particular day. Such as:

Set_information ('2021-12-1', 'test ') set_information('2021-12-1',' date ')Copy the code

Simple code explained above, detailed code can be downloaded in the public background calendar view.

Three, run the code

To run this code, you simply need to enter the project source folder in the local CMD /terminal directory and run:

​
​python calendary.py
Copy the code

It automatically generates an Excel spreadsheet called my_calendary. XLSX, and that’s the calendar we’ve generated.

How to extract calendar into pictures from inside?

Simply copy the part you want and paste it into any chat window to make a picture!

You can read the code and experiment with background colors, border colors, alternate colors, and images to your liking.

That’s how to generate a calendar in Python, and you can give it a try!

​end

This calendar is made to share this to like youyoumeng can do a good job for the people like ~ remember sanlian oh! The support of my family is the biggest motivation for me to update

To get the full source code with Python learning materials, please send me a private message or click on this line of font