Home  >  Article  >  Backend Development  >  Use Python to make my nucleic acid testing calendar

Use Python to make my nucleic acid testing calendar

王林
王林forward
2023-05-10 14:19:06846browse

Use Python to make my nucleic acid testing calendar

My coordinates are Shenzhen. Since 2022, most of the time it requires 24 hours, a few times it requires 48 hours, and even less often it requires 72 hours. There is no longer situation.

This article is made into a calendar based on my nucleic acid test records, and the nucleic acid test records are visualized in the calendar.

Enter data

The earliest time range that can be found for nucleic acid test records is one month. Previous test records were not saved in advance, so the data from August were used first. calendar.

Query the detection records in August and enter them into the code.

# coding=utf-8
from datetime import datetime
# 核酸检测数据,1表示当天做了核酸,0表示当天未做核酸
my_nucleic = {
 'date': [datetime.strftime(datetime(2022, 8, i+1), '%Y-%m-%d') for i in range(31)],
 'nucleic': [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
}

If nucleic acid was done on the same day, it is represented by 1. If nucleic acid was not done on that day, it is represented by 0.

The August date is generated using the Python standard library datetime.

Making a calendar

This article uses the Python library openpyxl to generate a calendar in an excel table.

1. Use openpyxl to create a table

import openpyxl
# 创建一个workbook对象,而且会在workbook中至少创建一个表worksheet
wb = openpyxl.Workbook()
# 获取当前活跃的worksheet,默认就是第一个worksheet
ws = wb.active

openpyxl is a library in Python for reading and writing excel files. You can use it after pip install openpyxl is installed.

2. Define the function of table initialization and cell setting

from openpyxl.styles import PatternFill, Font, Alignment, Border, Side
def init_sheet(ws):
 for r in range(100):
 for c in range(100):
 ws.cell(row=r+1, column=c+1).fill = PatternFill('solid', fgColor='000000')
def set_cell_style(ws, r, c, color):
 ws.cell(row=r, column=c).fill = PatternFill('solid', fgColor=color)
 ws.cell(row=r, column=c).font = Font(name="微软雅黑", size=14, bold=True)
 ws.cell(row=r, column=c).alignment = Alignment(horizontal='right', vertical='center')
 side = Side(style="medium", color="004B3C")
 ws.cell(row=r, column=c).border = Border(top=side, bottom=side, left=side, right=side)

Define a function that fills the table color with white, initializes the table, and sets the background to Pure white, the calendar looks more beautiful.

Define a function for processing cell format, and then directly call the function to format the cell for easy reuse.

3. Realize the calendar

import calendar
# 将表格填充成白色
init_sheet(ws)
# 设置年月单元格的边框
side = Side(style="medium", color="004B3C")
for col in range(7):
 ws.cell(row=1, column=col+1).border = Border(top=side, bottom=side, left=side, right=side)
# 合并年月单元格
ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=7)
# 写入内容和设置格式
ws.cell(row=1, column=1).value = '2022年8月'
set_cell_style(ws, r=1, c=1, color='418CFA')
# 写入星期一至星期日,并设置格式
title_data = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
for col in range(7):
 ws.cell(row=2, column=col+1).value = title_data[col]
 set_cell_style(ws, r=2, c=col+1, color='418CFA')
# 获取一个月的天数和第一天是星期几
monthday = calendar.monthrange(2022, 8)
# 设置日历的日期
col, row = monthday[0], 3
for i in range(len(my_nucleic['date'])):
 if col < 7:
 ws.cell(row=row, column=col + 1).value = i+1
 col += 1
 else:
 col = 0
 row += 1
 ws.cell(row=row, column=col + 1).value = i+1
 col += 1
 # 设置单元格格式
 set_cell_style(ws, r=row, c=col, color='000000')
 # 根据核酸结果填充颜色
 if my_nucleic['nucleic'][i] == 1:
 ws.cell(row=row, column=col).fill = PatternFill('solid', fgColor='009B3C')
# 设置行高
for i in range(1, row+1):
 ws.row_dimensions[i].height = 30
# 保存表格
wb.save(filename='show_august_nucleic.xlsx')
wb.close()

Calendar effect:

Use Python to make my nucleic acid testing calendar

You can see that August I only did 4 days without nucleic acid, and most of the time I kept it 24 hours.

Code implementation introduction:

  • First merge the first 7 columns of cells in the first row, write the year and month, and then in the second row from left to right Write Monday to Sunday and format it.
  • Use Python's calendar library calendar to return the day of the week that the first day of the current month is, and then determine the starting position of the 1st of the calendar.
  • Starting from the 1st, write the dates in excel sequentially. When the column reaches Sunday, wrap the row and return to the Monday column.
  • Fill the background color of the cells according to whether nucleic acid was done that day. In this article, if nucleic acid is done on that day, the background of the date is set to green (the color of the 24-hour nucleic acid code). If no nucleic acid is done, the background of the date is set to white.
  • Finally, save the results to an excel file. Open the excel file to see the created calendar.

Making a one-year calendar

After making a one-month calendar, continue to expand and make a one-year calendar. Let’s take a look at the effect first:

Use Python to make my nucleic acid testing calendar

Use Python to make my nucleic acid testing calendar

Implementation method introduction:

  • Data supplement, since only one month can be found nucleic acid records, so except for August 2022, the data for other months in this article are generated with random numbers.
  • Encapsulate the code for making a one-month calendar and pass in the year and month to generate a calendar for any month.
  • In the excel file, I design a row to display several months, and this article displays a row for 3 months. And calculate the starting cell position of each month's calendar.
  • Finally, enter the year, and then create a calendar for 12 months of the year and display it on one page. As long as you have the data, you can visualize the calendar for any year. (The code is long, you can get the complete code at the end of the article)

Another display method by year:

from pyecharts import options as opts
from pyecharts.charts import Calendar
import pandas as pd
nucleic_df = pd.DataFrame()
for i in range(12):
 month_nucleic = made_data(2022, i+1)
 month_df = pd.DataFrame(month_nucleic)
 nucleic_df = pd.concat([nucleic_df, month_df])
data = [[row_data['date'], row_data['nucleic']] for row_index, row_data in nucleic_df.iterrows()]
cal = Calendar(init_opts=opts.InitOpts(width='900px', height='500px'))
cal.add(
 '', data, calendar_opts=opts.CalendarOpts(range_="2022",
 daylabel_opts=opts.CalendarDayLabelOpts(first_day=1, name_map='cn'))
).set_series_opts(
 label_opts=opts.LabelOpts(font_size=12)
).set_global_opts(
 title_opts=opts.TitleOpts(title='核酸检测日历', pos_left='450', pos_top='0',
 title_textstyle_opts=opts.TextStyleOpts(color='black', font_size=16)),
 visualmap_opts=opts.VisualMapOpts(
 max_=1, min_=0, orient="horizontal", is_piecewise=False,
 range_color=["white", "white", "green"], pos_top="250px", pos_left='50px'
 ),
).render('my_nucleic.html')

Calendar effect:

Use Python to make my nucleic acid testing calendar

The Calendar component in pyecharts can also realize calendar visualization, but the format is relatively fixed and the display is relatively dense.

Summary

  • This article uses python to create a calendar for nucleic acid testing to visualize the number of days for nucleic acid testing.
  • This article uses two methods to display the calendar for one year.
  • If you need the source code, directly enter the official account background: assistant, password "nucleic acid calendar" to get the complete code.

The above is the detailed content of Use Python to make my nucleic acid testing calendar. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete