docx_other_func.py 4.0 KB

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