新增pandas读取excel
This commit is contained in:
@@ -2,8 +2,8 @@ import sys
|
|||||||
from PySide6 import QtCore, QtGui, QtWidgets
|
from PySide6 import QtCore, QtGui, QtWidgets
|
||||||
from PySide6.QtUiTools import QUiLoader
|
from PySide6.QtUiTools import QUiLoader
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from PySide6.QtWidgets import QFileDialog, QMainWindow
|
from PySide6.QtWidgets import QFileDialog, QMainWindow, QMessageBox, QLabel
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
class MainWindows(QMainWindow):
|
class MainWindows(QMainWindow):
|
||||||
UI_FILE = 'ui/main_window.ui'
|
UI_FILE = 'ui/main_window.ui'
|
||||||
@@ -12,6 +12,7 @@ class MainWindows(QMainWindow):
|
|||||||
|
|
||||||
self.ui = QUiLoader().load(self.UI_FILE)
|
self.ui = QUiLoader().load(self.UI_FILE)
|
||||||
|
|
||||||
|
|
||||||
self.ui.SelectFileBtn.clicked.connect(self.select_excel_file)
|
self.ui.SelectFileBtn.clicked.connect(self.select_excel_file)
|
||||||
|
|
||||||
# 点击选择文件按钮后,弹出选择EXCEL窗口。
|
# 点击选择文件按钮后,弹出选择EXCEL窗口。
|
||||||
@@ -25,10 +26,23 @@ class MainWindows(QMainWindow):
|
|||||||
# 选择EXCEL文件
|
# 选择EXCEL文件
|
||||||
file_path, _ = QFileDialog.getOpenFileName(None, caption, start_dir, filters)
|
file_path, _ = QFileDialog.getOpenFileName(None, caption, start_dir, filters)
|
||||||
|
|
||||||
if file_path:
|
if not file_path:
|
||||||
self.ui.FileNameEdt.setText(str(file_path))
|
return
|
||||||
print(file_path)
|
|
||||||
|
|
||||||
|
self.ui.FileNameEdt.setText(str(file_path))
|
||||||
|
|
||||||
|
#使用pandas读取EXCEL文件
|
||||||
|
try:
|
||||||
|
# 读取第一行表头
|
||||||
|
df = pd.read_excel(file_path, nrows=0) # 不加载数据,只加载列名
|
||||||
|
headers = list(df.columns)
|
||||||
|
except Exception as e:
|
||||||
|
QMessageBox.critical(self,"错误",f"读取文件失败:\n{e}")
|
||||||
|
return
|
||||||
|
#动态创建标签
|
||||||
|
for col in headers:
|
||||||
|
lbl = QLabel(f"· {col}")
|
||||||
|
self.ui.addWidget()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
UI_FILE = "uv/main_window.ui"
|
UI_FILE = "uv/main_window.ui"
|
||||||
|
|||||||
Reference in New Issue
Block a user