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:
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:
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:
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!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor