怎样将matplotlib生成的图形通过Flask输出到页面
立即下载
资源介绍:
matplotlib生成的图片通过flask输出到页面
"""
通过xlrd 1.2.0 读取xls,xlsx文件,获取表头字典,表头+数据,表头+类型+数据
"""
import xlrd, os
from xlrd import xldate_as_tuple
from datetime import datetime,time
# from openpyxl import load_workbook
import re
# import ctypetotbcolstype as ctotb #import is_int, is_float, is_dateordatetime, is_intorfloat, is_intphone
def is_dateordatetime(value):
if not isinstance(value, str):
return 'err'
value =value.strip()
try:
date_object = datetime.strptime(value, '%Y-%m-%d')
return 'date'
except ValueError:
try:
date_object = datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
return 'datetime'
except ValueError:
try:
date_object = datetime.strptime(value, '%H:%M:%S')
return 'time'
except ValueError:
return 'err'
return 'err'
def is_intphone(value):
# 参考 Python-输入一串字符,判断是否为手机号码 https://blog.csdn.net/weixin_57255032/article/details/128620752
# hmd = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178,
# 130, 131, 132, 155, 156, 185, 186, 145, 176, 179,
# 133, 153, 180, 181, 189, 177] # 列表
# -----排除手机号------
phone = str.strip(value)
if phone.isnumeric(): # 判断Phone是否全部都是数字字符
if len(phone) == 11: # 判断手机号是否为11位
# if int(phone[0:3]) in hmd: # 如果输入的手机号前三位数字在列表中,则输出"是一个有效号码"
# print(f'是电话:{phone}')
return 'phone'
else:
return 'err'
return 'err'
# def is_int(value):
# if isinstance(value, int):
# return 'int'
# if isinstance(value, str):
# try:
# int_number = int(value)
# return 'int'
# except ValueError:
# return 'err'
# else:
# return 'err'
#
# def is_float(value):
# if isinstance(value, float):
# return 'float'
# if isinstance(value, str):
# try:
# float_number = int(value) #---字符串如果是int也会被判断为float,所以在此先判断是否为int
# return 'int'
# except ValueError:
# try:
# float_number = float(value)
# return 'float'
# except ValueError:
# return 'err'
# else:
# return 'err'
#--------------判断是数值型,包括 int,float
def is_intorfloat(value):
# print('2024-03-01:',value)
if isinstance(value, int):
return 'int'
if isinstance(value, float):
return 'float'
if isinstance(value, str):
try:
int_number = int(value)
return 'int'
except ValueError:
try:
float_number = float(value)
return 'float'
except ValueError:
return 'err'
return 'err'
class ExcelUtil:
def __init__(self, excel_path, sheet_name=None):
# print("---------excel_path=",excel_path)
self.excel_path = excel_path
# self.wb = load_workbook(excel_path, data_only=True)
self.data = xlrd.open_workbook(excel_path)
# 如果sheet_name传入值了并且名字在表格的sheet表名里就用这个名的表,如果没有就默认选择第一个表格
if sheet_name != None: # and sheet_name in self.wb.sheetnames:
self.table = self.data.sheet_by_name(sheet_name)
# self.ws = self.wb[sheet_name]
else:
self.table = self.data.sheets()[0]
# print("-------self.table=",type(self.table),self.table)
# self.ws = self.wb.worksheets[0]
try:
# 获取第一行作为key值
self.keys = self.table.row_values(0)
except:
print()
# 获取总行数
self.rowNum = self.table.nrows
# 获取总列数
self.colNum = self.table.ncols
self.ctypeDice={0: "empty",1: "string", 2: "number", 3: "date", 4: "boolean", 5: "error"}
def getKeys(self):
return self.keys
def getRowNum(self):
return self.rowNum
def getColNum(self):
return self.colNum
# 获取表格的表头+数据字典
def dict_data(self):
if self.rowNum <= 1:
print("总行数小于1,请核实表格数据!")
os._exit(0)
else:
r = []
# 行数,从第2行取数据,第一行为表头,如果从第一行取数据,1改为0或不写
for row in range(1, self.rowNum):
s = {}
# 列数
for col in range(self.colNum):
ctype = self.table.cell(row, col).ctype
cell = self.table.cell_value(row, col)
print(ctype,cell)
# 如果表格中是日期,那么要进行转化
if ctype == 3:
# print(f'excel中date型内容:{cell}')
aa =xldate_as_tuple(cell,0)
# print(f"元组{aa}")#返回的是一个元组(2023, 6, 26, 18, 35, 45)
if aa[0] == 0:
time_value = time(*aa[3:])
cell = time_value.strftime('%H:%M:%S') #时间型
else:
date = datetime(*xldate_as_tuple(cell, 0))
cell = date.strftime('%Y-%m-%d %H:%M:%S')
# print(f"转换的日期时间:{cell}") #
s[self.keys[col]] = cell
# 如果是整型
elif ctype == 2 and cell % 1 == 0:
s[self.keys[col]] = int(cell)
else:
s[self.keys[col]] = cell
r.append(s)
# j=1代表第二行
# j = 1
# i控制循环次数,也就是取多少行数据
# for i in range(self.rowNum - 1):
# s = {}
# # 从第二行取对应values值
# values = self.table.row_values(j)
# # 列数
# for x in range(self.colNum):
# # 如果表格中是日期,那么要进行转化
# ctype = self.table.cell(j, x).ctype
# cell = self.table.cell_value(j, x)
# if ctype == 3:
# date = datetime(*xldate_as_tuple(cell, 0))
# cell = date.strftime('%Y-%m-%d %H:%M')
# s[self.keys[x]] = cell
# # 如果是整型
# elif ctype == 2 and cell % 1 ==0:
# s[self.keys[x]] = int(cell)
# else:
# s[self.keys[x]] = values[x]
# r.append(s)
# j += 1
return r
# 获取表格的表头+数据字典
def dict_sheet0(self):
if self.rowNum <= 1:
print("总行数小于1,请核实表格数据!")
os._exit(0)
else:
r = []
# 行数,从第2行取数据,第一行为表头,如果从第一行取数据,1改为0或不写
for row in range(0, self.rowNum):
s = {}
# 列数
for col in range(self.colNum):
ctype = self.table.cell(row, col).ctype
cell = self.table.cell_value(row, col)
# 如果表格中是日期,那么要进行转化
if ctype == 3: