Home >Backend Development >Python Tutorial >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.
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.
This article uses the Python library openpyxl to generate a calendar in an excel 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.
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.
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:
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:
After making a one-month calendar, continue to expand and make a one-year calendar. Let’s take a look at the effect first:
Implementation method introduction:
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:
The Calendar component in pyecharts can also realize calendar visualization, but the format is relatively fixed and the display is relatively dense.
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!