pyqt5固定打开一个excel文件

时间: 2023-10-07 08:13:26 浏览: 53
以下是一个使用PyQt5固定打开一个Excel文件的示例代码: ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QMessageBox, QFileDialog from PyQt5.QtCore import Qt from PyQt5.QtGui import QIcon from PyQt5.QtChart import QChart, QChartView, QLineSeries, QValueAxis from openpyxl import load_workbook class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("固定打开 Excel 文件并绘制折线图") self.setGeometry(100, 100, 800, 600) # 添加菜单栏 menubar = self.menuBar() filemenu = menubar.addMenu('文件') openfile = filemenu.addAction('打开文件') openfile.triggered.connect(self.openFile) # 添加工具栏 toolbar = self.addToolBar('工具栏') toolbar.setMovable(False) openfile = QAction(QIcon('open.png'), '打开文件', self) openfile.triggered.connect(self.openFile) toolbar.addAction(openfile) # 创建图表 self.chart = QChart() self.chart.setTitle("折线图") self.chart.setAnimationOptions(QChart.SeriesAnimations) # 创建图表视图并设置图表 chartview = QChartView(self.chart, self) chartview.setRenderHint(QPainter.Antialiasing) self.setCentralWidget(chartview) # 固定打开文件 self.openExcelFile('data.xlsx') def openFile(self): # 弹出文件选择对话框 filename, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "Excel 文件 (*.xlsx)") if filename: self.openExcelFile(filename) def openExcelFile(self, filename): # 加载 Excel 文件 try: wb = load_workbook(filename=filename, read_only=True) except: QMessageBox.warning(self, "错误", "无法打开文件: %s" % filename, QMessageBox.Ok) return sheet = wb.active # 获取数据 data = [] for row in sheet.iter_rows(min_row=2, values_only=True): data.append(row) # 绘制折线图 self.chart.removeAllSeries() for i in range(1, sheet.max_column): series = QLineSeries() series.setName(sheet.cell(row=1, column=i+1).value) for j in range(len(data)): series.append(j, data[j][i]) self.chart.addSeries(series) # 设置 X 轴和 Y 轴 axisx = QValueAxis() axisx.setRange(0, len(data)) axisx.setLabelFormat("%d") axisx.setTickCount(len(data)) axisx.setTitleText(sheet.cell(row=1, column=1).value) axisx.setGridLineVisible(True) self.chart.addAxis(axisx, Qt.AlignBottom) for series in self.chart.series(): self.chart.setAxisX(axisx, series) axisy = QValueAxis() axisy.setRange(sheet.cell(row=2, column=2).value, sheet.cell(row=sheet.max_row, column=2).value) axisy.setLabelFormat("%.1f") axisy.setTickCount(10) axisy.setTitleText(sheet.cell(row=1, column=2).value) axisy.setGridLineVisible(True) self.chart.addAxis(axisy, Qt.AlignLeft) for series in self.chart.series(): self.chart.setAxisY(axisy, series) # 更新图表视图 self.chart.legend().setVisible(True) self.chart.legend().setAlignment(Qt.AlignBottom) chartview = self.centralWidget() chartview.setChart(self.chart) if __name__ == '__main__': app = QApplication(sys.argv) mainwin = MainWindow() mainwin.show() sys.exit(app.exec_()) ``` 该程序在MainWindow类的构造函数中添加了一个openExcelFile方法,该方法用于固定打开指定的Excel文件。程序还在工具栏中添加了一个“打开文件”按钮,并将其绑定到openFile方法。 当用户点击“打开文件”按钮或选择“打开文件”选项时,程序会弹出文件选择对话框,用户可以选择一个Excel文件。如果用户选择了一个文件,则程序会调用openExcelFile方法打开该文件并绘制折线图。 如果用户未选择文件,程序会默认打开名为“data.xlsx”的文件。如果程序无法打开指定的文件,则会弹出一个警告消息框。

相关推荐

最新推荐

recommend-type

pyqt5 实现 下拉菜单 + 打开文件的示例代码

今天小编就为大家分享一篇pyqt5 实现 下拉菜单 + 打开文件的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

开发第一个基于PyQt5的桌面应用

这两个类都位于PyQt5.QtWidgets中,所以需要导入这个模块。以及系统模块sys。 import sys # 系统模块,获得命令行参数 from PyQt5.QtWidgets import QApplication, QWidget, QLabel # 导入QAppliaction,QLabel以及...
recommend-type

python3使用pyqt5制作一个超简单浏览器的实例

下面小编就为大家带来一篇python3使用pyqt5制作一个超简单浏览器的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

PyQt5实现从主窗口打开子窗口的方法

今天小编就为大家分享一篇PyQt5实现从主窗口打开子窗口的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

ubuntu 安装pyqt5和卸载pyQt5的方法

安装pyqt5 wind@wind-ThinkPad-X250:~/Downloads/PyQt5_gpl-5.12.2$ python3 -m pip install PyQt5 Collecting PyQt5 Downloading ...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。