|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是代码
- #!/usr/bin/env python
- # encoding: utf-8
- # author: ricia
- import openpyxl
- import tkinter as tk
- import pyecharts.options as opts
- from tkinter import filedialog
- from pyecharts.charts import Line
- # 主窗口
- window = tk.Tk()
- window.title('Excel绘图小程序')
- window.geometry('600x400')
- # 收集用户数据
- var_min_row = tk.IntVar()
- var_max_row = tk.IntVar()
- var_min_col = tk.IntVar()
- var_max_col = tk.IntVar()
- var_x_number = tk.IntVar()
- var_y_number = tk.IntVar()
- tk.Label(window, text='开始行号:', font=14).place(x=20, y=50)
- min_row = tk.Entry(window, textvariable=var_min_row, show=None, font=('Arial', 14), width=8).place(x=120, y=50)
- tk.Label(window, text='结束行号:', font=14).place(x=20, y=80)
- max_row = tk.Entry(window, textvariable=var_max_row, show=None, font=('Arial', 14), width=8).place(x=120, y=80)
- tk.Label(window, text='开始列号:', font=14).place(x=20, y=110)
- min_col = tk.Entry(window, textvariable=var_min_col, show=None, font=('Arial', 14), width=8).place(x=120, y=110)
- tk.Label(window, text='结束列号:', font=14).place(x=20, y=140)
- max_col = tk.Entry(window, textvariable=var_max_col, show=None, font=('Arial', 14), width=8).place(x=120, y=140)
- tk.Label(window, text='选中区域后,指定X,Y在新区域对应的列', font=12).place(x=20, y=200)
- tk.Label(window, text='绘图x轴列数:', font=14).place(x=20, y=240)
- x_number = tk.Entry(window, textvariable=var_x_number, show=None, font=('Arial', 14), width=8).place(x=150, y=240)
- tk.Label(window, text='绘图y轴列数:', font=14).place(x=20, y=270)
- y_number = tk.Entry(window, textvariable=var_y_number, show=None, font=('Arial', 14), width=8).place(x=150, y=270)
- # 提示消息显示框
- var = tk.StringVar()
- message = tk.Label(window, textvariable=var, bg='green', fg='white', font=('Arial', 12), width=30, height=3)
- message.place(x=280, y=80)
- # 读取文件数据
- def open_file():
- global file_path
- global x_data
- global y_data
- # 初始化
- file_path = ''
- x_data = []
- y_data = []
- # 获取用户输入的值
- min_row = var_min_row.get()
- max_row = var_max_row.get()
- min_col = var_min_col.get()
- max_col = var_max_col.get()
- x_number = var_x_number.get()
- y_number = var_y_number.get()
- # 判断数据是否都已经输入
- if min_row and max_row and min_col and max_col and x_number and y_number != None:
- # 选择需要打开的文件
- file_path = filedialog.askopenfilename()
- # 检测文件格式是否是openpyxl支持打开的格式
- file_type =['xlsx','xlsm','xltx','xltm']
- if str(file_path).split('.')[1] in file_type:
- # 打开文件
- wb = openpyxl.load_workbook(file_path)
- # 获取excel文件的第一个工作表
- ws = wb[wb.sheetnames[0]]
- # 读取数据
- for each_row in ws.iter_rows(min_row=min_row, min_col=min_col, max_row=max_row, max_col=max_col):
- x_data.append(each_row[x_number-1].value)
- y_data.append(each_row[y_number-1].value)
- var.set('文件已读取成功!')
- else:
- var.set('请输入Excel格式!')
- else:
- var.set('请先输入所有行列号!')
- # 打开文件按钮
- open_bt = tk.Button(window, text='打开文件', font=14, width=15, height=2, command=open_file).place(x=350,y=240)
- # 画平滑折线图
- def line_draw():
- # js_host 生成html文件本地读取js文件用 可以不设置
- line = Line(init_opts=opts.InitOpts(js_host='../js/', width='1000px'))
- line.add_xaxis(x_data)
- line.add_yaxis('S/Ø', y_data, is_smooth=True)
- # 全局设置
- line.set_global_opts(toolbox_opts=opts.ToolboxOpts(),
- datazoom_opts=opts.DataZoomOpts(range_start=0,range_end=100),
- tooltip_opts=opts.TooltipOpts(trigger='axis'),
- xaxis_opts=opts.AxisOpts(
- axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
- is_scale=False,
- boundary_gap=False,
- name='时间'
- ))
- # 系列设置
- line.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
- picture = line.render('draw.html')
- var.set('绘图完成!')
- return picture
- # 绘图按钮
- draw_bt = tk.Button(window, text='开始绘图', font=14, width=15, height=2, command=line_draw).place(x=350,y=300)
- # 主窗口循环
- window.mainloop()
复制代码
最后扔给我一个报错信息,
Failed to execute script test
是和pyecharts有关,建议你看看使用到pyecharts
的代码
|
|