docx_other_func.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import io
  4. import time
  5. from base64 import b64decode
  6. from functools import wraps
  7. import matplotlib.pyplot as plt
  8. import qrcode
  9. from docx.shared import RGBColor
  10. plt.switch_backend('Agg')
  11. from io import BytesIO
  12. from tools.loglog import logger, log_err_e
  13. from threading import Lock
  14. from config.read_config import address
  15. lock = Lock()
  16. width_cm, height_cm = 5.4, 3
  17. width_in = width_cm
  18. height_in = height_cm
  19. plt.figure(figsize=(width_in, height_in))
  20. def hex_to_rgb(hex_color: str):
  21. hex_color = hex_color.lstrip('#')
  22. return RGBColor(int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16))
  23. def rgb_to_hex(r, g, b):
  24. return '{:02x}{:02x}{:02x}'.format(r, g, b)
  25. def is_base64(text):
  26. try:
  27. image_bytes = b64decode(text)
  28. return image_bytes
  29. except Exception:
  30. return False
  31. def time_use(fn):
  32. @wraps(fn)
  33. def cc(*args, **kwargs):
  34. f_time = time.time()
  35. res = fn(*args, **kwargs)
  36. cha = round(time.time() - f_time, 3)
  37. if cha > 0.3:
  38. print(f'函数:{fn.__name__} 一共用时', cha, '秒')
  39. return res
  40. return cc
  41. def qrcode_maker(id_text=None, full_url=None) -> BytesIO:
  42. """
  43. :param id_text: id_text 提供id,二维码地址是春笋筛查表的地址;http://dcjxb.yunzhixue.cn/link?type=scanpage&id=999;
  44. :param full_url: 如果提供,直接使用这个文本来生成二维码的地址
  45. :return: 二维码图的IO对象,记得关闭
  46. """
  47. text = f"{address}/link?type=scanpage&id={id_text}"
  48. if full_url:
  49. text = full_url
  50. qr = qrcode.QRCode(
  51. version=1,
  52. error_correction=qrcode.constants.ERROR_CORRECT_L,
  53. box_size=12,
  54. border=4,
  55. )
  56. qr.add_data(text)
  57. qr.make(fit=True)
  58. img = qr.make_image(fill_color="black", back_color="white")
  59. img_byte_arr = io.BytesIO()
  60. img.save(img_byte_arr, format='PNG')
  61. img_byte_arr.seek(0)
  62. return img_byte_arr
  63. def get_weekday():
  64. today = datetime.date.today()
  65. weekday_index = today.weekday()
  66. weekdays_chinese = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
  67. weekday_chinese = weekdays_chinese[weekday_index]
  68. return weekday_chinese
  69. def make_chart(x_axis_data, y_axis_datas, title, sub_title_list, x_axis_label=None, y_axis_label=None):
  70. """
  71. :param sub_title_list: 小标题集合,放在右上角,用来标记每个y轴的数据标题
  72. :param y_axis_label:Y轴文本
  73. :param x_axis_label:X轴文本
  74. :param title:图标标题
  75. :param x_axis_data: X数据列表,单列表数据,一般就日期。
  76. :param y_axis_datas: Y数据列表[合集],是个大列表。因为可能会有多个y线
  77. :return:
  78. """
  79. x_len = len(x_axis_data)
  80. image_io = BytesIO()
  81. font1 = {'family': 'SimSun', 'weight': 'normal', 'size': 14}
  82. plt.rc('font', **font1)
  83. plt.rcParams["axes.unicode_minus"] = False
  84. try:
  85. for y in y_axis_datas:
  86. if len(y) != x_len:
  87. logger.error("x轴的y轴的数据个数不一致")
  88. plt.plot(x_axis_data, y, marker='o', label="zxs")
  89. plt.title(title)
  90. if x_axis_label:
  91. plt.xlabel(x_axis_label)
  92. if y_axis_label:
  93. plt.ylabel(y_axis_label)
  94. plt.grid(True)
  95. for index, sub_title in enumerate(sub_title_list):
  96. plt.text(0.95, 0.9 - index * 0.15, sub_title, transform=plt.gca().transAxes, fontsize=10, va='top', ha='right', backgroundcolor='w')
  97. with lock:
  98. plt.savefig(image_io, format='png', bbox_inches='tight')
  99. image_io.seek(0)
  100. return image_io
  101. except Exception as e:
  102. log_err_e(e, "折线图生成错误")
  103. image_io.close()
  104. return None
  105. if __name__ == '__main__':
  106. t = time.time()
  107. io = qrcode_maker('', "http://111.231.167.191:8001/mp3")
  108. with open("1.jpg", 'wb') as f:
  109. f.write(io.read())
  110. print(time.time() - t)