pyqt5根据excel表生成表格控件
时间: 2023-07-05 08:05:31 浏览: 168
你可以使用 Pandas 库来读取 Excel 文件,然后将其转换为 PyQt5 的表格控件。以下是一个简单的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem
import pandas as pd
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 读取 Excel 文件
df = pd.read_excel('example.xlsx')
# 创建表格控件
table = QTableWidget(self)
table.setRowCount(df.shape[0])
table.setColumnCount(df.shape[1])
# 将数据添加到表格控件中
for i in range(df.shape[0]):
for j in range(df.shape[1]):
item = QTableWidgetItem(str(df.iloc[i, j]))
table.setItem(i, j, item)
self.setCentralWidget(table)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
你需要将 `example.xlsx` 替换为你自己的 Excel 文件路径。此代码将 Excel 文件中的数据读取到 Pandas 数据框中,然后将其添加到 PyQt5 的表格控件中。
阅读全文
相关推荐















