123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- # -*- coding: utf-8 -*-
- from functools import wraps
- import time
- import io
- import qrcode
- from docx.shared import RGBColor
- from base64 import b64decode
- import datetime
- import matplotlib.pyplot as plt
- plt.switch_backend('Agg')
- from io import BytesIO
- from tools.loglog import logger, log_err_e
- from docx import Document
- from docx.shared import Inches,Cm
- from threading import Lock
- from config.read_config import address
- lock = Lock()
- width_cm, height_cm = 5.4, 3
- width_in = width_cm
- height_in = height_cm
- plt.figure(figsize=(width_in, height_in))
- def hex_to_rgb(hex_color:str):
- hex_color = hex_color.lstrip('#')
- return RGBColor(int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16))
- def rgb_to_hex(r, g, b):
- return '{:02x}{:02x}{:02x}'.format(r, g, b)
- def is_base64(text):
- try:
-
- image_bytes =b64decode(text)
- return image_bytes
- except Exception:
-
- return False
- def time_use(fn):
- @wraps(fn)
- def cc(*args,**kwargs):
- f_time = time.time()
- res = fn(*args,**kwargs)
- cha = round(time.time()-f_time,3)
- if cha > 0.3:
- print(f'函数:{fn.__name__} 一共用时',cha,'秒')
- return res
- return cc
- def qrcode_maker(id_text=None,full_url=None) -> BytesIO:
- """
- :param id_text: id_text 提供id,二维码地址是春笋筛查表的地址;http://dcjxb.yunzhixue.cn/link?type=scanpage&id=999;
- :param full_url: 如果提供,直接使用这个文本来生成二维码的地址
- :return: 二维码图的IO对象,记得关闭
- """
- text = f"{address}/link?type=scanpage&id={id_text}"
- if full_url:
- text = full_url
- qr = qrcode.QRCode(
- version=1,
- error_correction=qrcode.constants.ERROR_CORRECT_L,
- box_size=12,
- border=4,
- )
- qr.add_data(text)
- qr.make(fit=True)
-
- img = qr.make_image(fill_color="black", back_color="white")
- img_byte_arr = io.BytesIO()
- img.save(img_byte_arr, format='PNG')
- img_byte_arr.seek(0)
-
- return img_byte_arr
- def get_weekday():
- today = datetime.date.today()
- weekday_index = today.weekday()
- weekdays_chinese = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
- weekday_chinese = weekdays_chinese[weekday_index]
- return weekday_chinese
- def make_chart(x_axis_data,y_axis_datas,title,sub_title_list,x_axis_label=None,y_axis_label=None):
- """
- :param sub_title_list: 小标题集合,放在右上角,用来标记每个y轴的数据标题
- :param y_axis_label:Y轴文本
- :param x_axis_label:X轴文本
- :param title:图标标题
- :param x_axis_data: X数据列表,单列表数据,一般就日期。
- :param y_axis_datas: Y数据列表[合集],是个大列表。因为可能会有多个y线
- :return:
- """
- x_len = len(x_axis_data)
-
- image_io = BytesIO()
- font1 = {'family': 'SimSun', 'weight': 'normal', 'size': 14}
- plt.rc('font', **font1)
- plt.rcParams["axes.unicode_minus"] = False
- try:
- for y in y_axis_datas:
- if len(y) != x_len:
- logger.error("x轴的y轴的数据个数不一致")
- plt.plot(x_axis_data, y, marker='o',label="zxs")
- plt.title(title)
- if x_axis_label:
- plt.xlabel(x_axis_label)
- if y_axis_label:
- plt.ylabel(y_axis_label)
- plt.grid(True)
- for index,sub_title in enumerate(sub_title_list):
- plt.text(0.95, 0.9-index*0.15, sub_title, transform=plt.gca().transAxes, fontsize=10, va='top', ha='right', backgroundcolor='w')
- with lock:
- plt.savefig(image_io, format='png', bbox_inches='tight')
- image_io.seek(0)
- return image_io
- except Exception as e:
- log_err_e(e,"折线图生成错误")
- image_io.close()
- return None
- if __name__ == '__main__':
-
-
- t= time.time()
- io = qrcode_maker('',"http://111.231.167.191:8001/mp3")
- with open("1.jpg",'wb') as f:
- f.write(io.read())
- print(time.time()-t)
|