iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python一键生成核酸检测日历的操作代码
  • 260
分享到

Python一键生成核酸检测日历的操作代码

2024-04-02 19:04:59 260人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录UI自动化提取检测记录生成核酸检测日历大家好,我是小小明。鉴于深圳最近每天都要做核酸,我觉得有必要用程序生成自己的检测日历,方便查看。 首先我们需要从深i您-自主申报的微信小程序

大家好,我是小小明。鉴于深圳最近每天都要做核酸,我觉得有必要用程序生成自己的检测日历,方便查看。

首先我们需要从深i您-自主申报微信小程序中提取自己的核酸检测记录,然后使用绘图库自动绘制检测日历。

UI自动化提取检测记录

首先,我们在PC端微信打开深i您-自主申报微信小程序

点击 核酸检测记录,再点击自己的姓名即可查看自己的核酸检测记录:

下面我们打开inspect.exe工具分析查看节点:

于是开始编码:

import pandas as pd
import uiautomation as auto

pane = auto.PaneControl(searchDepth=1, Name="深i您 - 自主申报")
pane.SetActive(waitTime=0.01)
pane.SetTopmost(waitTime=0.01)

tags = pane.GetFirstChildControl().GetFirstChildControl() \
    .GetLastChildControl().GetFirstChildControl() \
    .GetChildren()
result = []
for tag in tags:
    tmp = []
    for item, depth in auto.WalkControl(tag, maxDepth=4):
        if item.ControlType != auto.ControlType.TextControl:
            continue
        tmp.append(item.Name)
    row = {"检测结果": tmp[1]}
    for k, v in zip(tmp[2::2], tmp[3::2]):
        row[k[:-1]] = v
    result.append(row)
pane.SetTopmost(False, waitTime=0.01)
df = pd.DataFrame(result)
df.采样时间 = pd.to_datetime(df.采样时间)
df.检测时间 = pd.to_datetime(df.检测时间)
df.head()

结果如下:

有了检测数据,我们就可以生成检测日历了。也可以先将检测数据保存起来:

df.to_excel(f"{pd.Timestamp('now').date()}核酸检测记录.xlsx", index=False)

注意:其他省市的童鞋,请根据自己城市对应的小程序实际情况编写代码进行提取。

原本想抓包获取检测数据,却发现新版的PC端微信的小程序已经无法被抓包,暂时还未理解啥原理啥实现的。

后面碰到正在检测的记录上面的代码会报错,分析节点后,下面升级到能够兼容出现检测记录的情况:

import pandas as pd
import uiautomation as auto

pane = auto.PaneControl(searchDepth=1, Name="深i您 - 自主申报")
pane.SetActive(waitTime=0.01)
pane.SetTopmost(waitTime=0.01)

tag = pane.GetFirstChildControl().GetFirstChildControl() \
    .GetLastChildControl().GetFirstChildControl()
tmp = []
for item, depth in auto.WalkControl(tag, maxDepth=2):
    if item.ControlType != auto.ControlType.TextControl:
        continue
    tmp.append(item.Name)
result = []
tags = tag.GetChildren()
if tmp:
    row = {"检测结果": tmp[1]}
    for k, v in zip(tmp[2::2], tmp[3::2]):
        row[k[:-1]] = v
    result.append(row)
for tag in tags:
    if tag.Name or tag.GetFirstChildControl().Name:
        continue
    tmp = []
    for item, depth in auto.WalkControl(tag, maxDepth=4):
        if item.ControlType != auto.ControlType.TextControl:
            continue
        tmp.append(item.Name)
    row = {"检测结果": tmp[1]}
    for k, v in zip(tmp[2::2], tmp[3::2]):
        row[k[:-1]] = v
    result.append(row)
pane.SetTopmost(False, waitTime=0.01)
df = pd.DataFrame(result)
df.采样时间 = pd.to_datetime(df.采样时间)
df.检测时间 = pd.to_datetime(df.检测时间)
df.head()

生成核酸检测日历

经过几小时的测试,最终编写出如下方法:

import calendar
from PIL import Image, ImageFont, ImageDraw


def create_calendar_img(year, month, days):
    "作者:小小明 https://xxmdmst.blog.csdn.net"
    font = ImageFont.truetype('msyh.ttc', size=20)
    im = Image.new(mode='RGB', size=(505, 251), color="white")
    draw = ImageDraw.Draw(im=im)
    draw.rectangle((0, 0, 504, 40), fill='#418CFA', outline='black', width=1)
    draw.text(xy=(170, 0), text=f"{year}年{month}月", fill=0,
              font=ImageFont.truetype('msyh.ttc', size=30))
    title_datas = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    for i, title_data in enumerate(title_datas):
        draw.rectangle((i*72, 40, (i+1)*72, 70),
                       fill='lightblue', outline='black', width=1)
        draw.text(xy=(i*72+7, 40), text=title_data, fill=0,
                  font=font)

    # 第一天是星期几和一个月的天数
    weekday, day_num = calendar.monthrange(year, month)
    col, row = weekday, 1
    for i in range(1, day_num+1):
        if col >= 7:
            col = 0
            row += 1
        fill = "#009B3C" if i in days else None
        draw.rectangle((col*72, 40+30*row, (col+1)*72, 40+30*(row+1)),
                       fill=fill, outline='black', width=1)
        draw.text(xy=(col*72+24, 40+30*row), text=str(i).zfill(2), fill=0,
                  font=font)
        col += 1
    return im

然后我们可以生成最近1-3个月的核酸检测日历:

dates = df.采样时间.dt.date.sort_values().astype(str)
for month, date_split in dates.groupby(dates.str[:7]):
    year, month = map(int, month.split("-"))
    days = date_split.str[-2:].astype(int)
    im = create_calendar_img(year, month, days.values)
    display(im)

可以看到我最近连续9天都是每天做一次核酸。

如果有需要,这些图片也可以按需保存:

im.save(f"{year}年{month}月检测记录.jpg")

到此这篇关于python一键生成核酸检测日历的文章就介绍到这了,更多相关Python生成核酸检测日历内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python一键生成核酸检测日历的操作代码

本文链接: https://www.lsjlt.com/news/120353.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作