Suck the cat with code! This paper is participating in[Cat Essay Campaign]
preface
Recently, cloud suction cat activities are hot, cats are so cute, take the time between work, make a 2022 cat themed calendar for yourself.
Without further ado, let’s get to work!
Download cat pictures
We searched the web for “cat” and found thousands of pictures of cats
Choose your favorite images, download them into a file, and name each image with a number from 1 to 12
Downloading dependent libraries
This time, we need to download the third-party dependency library – OpenPyXL to complete the operation of writing and editing excel table
-
Introduction to the OpenPyXL module
Openpyxl is a library for reading and writing Excel files
-
Openpyxl module download
pip install openpyxl Copy the code
Importing a dependency library
According to the calendar requirements: to generate the calendar and write to Excel, so we need to import the library
import openpyxl
# Calendar module
import calendar
Set the cell style
from openpyxl.styles import Alignment,PatternFill,Font
Get the letters of the column
from openpyxl.utils import get_column_letter
Write an image to Excel
from openpyxl.drawing.image import Image
Load the Excel file
from openpyxl import load_workbook
Copy the code
Generate a 12-month calendar
According to the demand analysis of the calendar, the approach is as follows
-
Start by creating a new one using OpenPyxl.workbook ()
wb = openpyxl.Workbook() Copy the code
-
Create the table using the Wb call create_sheet to the OpenPyxL. Workbook instantiation object
sheet = wb.create_sheet(index=0,title=sheet_name[i]) Copy the code
-
Use Calendar.MonthCalendar (year, I) to get the two-dimensional list data for the specified month
print(calendar.monthcalendar(2022.1)) # [[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.0.0.0.0.0.0]] Copy the code
-
Use two for loops to get a 2-d list of 12 months
for j in range(len(calendar.monthcalendar(year,i))): for k in range(len(calendar.monthcalendar(year,i)[j])): value = calendar.monthcalendar(year,i)[j][k] Copy the code
-
Condition judgement When the data obtained is 0, write blank in the table; Otherwise, write a specific date
f value == 0: value = ' ' sheet.cell(row=j+9,column=k+1).value=value else: sheet.cell(row=j+9,column=k+1).value=value sheet.cell(row=j+9,column=k+1).font=Font(U "Microsoft Yahei",size=11) Copy the code
-
Add title and style Settings for the calendar
- Because set calendar firstday set to day of the week for the firstday of the week
- The incoming calendar entitled “Sunday”, “Monday”, “Tuesday”, “on Wednesday”, “Thursday”, “Friday”, “Saturday”]
- Font is set to Microsoft Yahei and size is 11
- Horizontal =”right”,vertical=”center”
for R1 in range(1.100) :for C2 in range(1.100): sheet.cell(row=R1,column=C2).fill = fill days = ["Sunday"."Monday"."Tuesday"."Wednesday"."Thursday"."Friday"."Saturday"] num = 0 # Set the calendar cell style for k3 in range(1.8): sheet.cell(row=8,column=k3).value = days[num] sheet.cell(row=8, column=k3).alignment= align sheet.cell(row=8, column=k3).font = Font(U "Microsoft Yahei",size=11) sheet.column_dimensions[get_column_letter(k3)].width = 12 num +=1` ` `Copy the code
-
Finally, let’s set up the table format of the calendar
- Set the cell height to 30
- The cell font style is Microsoft Yahei
- Cells are horizontally centered
for k4 in range(8.15): sheet.row_dimensions[k4].height = 30 sheet.cell(row=3,column=1).value = year+"Year" sheet.cell(row=4,column=1).value = sheet_name[i] sheet.cell(row=3, column=1).font = Font(U "Microsoft Yahei", size=16,bold=True,color="FF7887") sheet.cell(row=4, column=1).font = Font(U "Microsoft Yahei", size=16, bold=True, color="FF7887") sheet.cell(row=3, column=1).alignment = align sheet.cell(row=4, column=1).alignment = align Copy the code
-
Save the generated calendar file
wb.save(filename) Copy the code
Add cat pictures
-
Wb.load_workbook () opens the calendar file
-
Wb.get_sheet_names () gets a list of Excel table names
-
The loaded file object uses the add_image() method to add the image to the corresponding table
def add_image(filepath) : wb = load_workbook(filepath) sheet_names = wb.get_sheet_names() for i in range(0.len(sheet_names)): wb = load_workbook(filepath) ws = wb[sheet_names[i]] ws.merge_cells('I1:P20') img = Image(f'file/{i}.jpg') ws.add_image(img,'I1') wb.save(filepath) Copy the code
Finally, let’s look at the effect
conclusion
Make a calendar with a cat theme in Python
That’s the content of this episode. Please give us your thumbs up and comments. See you next time