python读取excel文件并用pyqt5进行可视化展示
时间: 2023-07-18 13:40:30 浏览: 112
VTK_head-scan_visualization-:使用python VTK和pyqt5可视化人头
可以使用Python中的`openpyxl`库来读取Excel文件,并使用PyQt5进行可视化展示。以下是一个简单的示例代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
from openpyxl import load_workbook
class ExcelViewer(QMainWindow):
def __init__(self, filename):
super().__init__()
self.filename = filename
self.tableWidget = QTableWidget()
self.setCentralWidget(self.tableWidget)
self.load_file()
def load_file(self):
workbook = load_workbook(filename=self.filename)
sheet = workbook.active
row_count = sheet.max_row
column_count = sheet.max_column
self.tableWidget.setRowCount(row_count)
self.tableWidget.setColumnCount(column_count)
for row in range(1, row_count+1):
for col in range(1, column_count+1):
cell = sheet.cell(row=row, column=col)
self.tableWidget.setItem(row-1, col-1, QTableWidgetItem(str(cell.value)))
self.tableWidget.setHorizontalHeaderLabels([sheet.cell(row=1, column=i).value for i in range(1, column_count+1)])
self.tableWidget.setVerticalHeaderLabels([str(i) for i in range(1, row_count+1)])
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
viewer = ExcelViewer('example.xlsx')
sys.exit(app.exec_())
```
这个代码将会在窗口中展示Excel表格的内容。你可以根据需要修改代码以适应你的具体需求。
阅读全文