Files
MyPythonProject/excel_split.py
2025-11-19 17:09:04 +08:00

57 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import sys
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtUiTools import QUiLoader
from pathlib import Path
from PySide6.QtWidgets import QFileDialog, QMainWindow, QMessageBox, QLabel
import pandas as pd
class MainWindows(QMainWindow):
UI_FILE = 'ui/main_window.ui'
def __init__(self):
super(MainWindows, self).__init__()
self.ui = QUiLoader().load(self.UI_FILE)
self.ui.SelectFileBtn.clicked.connect(self.select_excel_file)
# 点击选择文件按钮后弹出选择EXCEL窗口。
def select_excel_file(self):
# 弹窗标题
caption = "请选择EXCEL文件"
# 起始目录
start_dir = str(Path.home())
# 文件类型过滤器
filters = "Excel 文件 (*.xlsx *.xls);;所有文件 (*.*)"
# 选择EXCEL文件
file_path, _ = QFileDialog.getOpenFileName(None, caption, start_dir, filters)
if not file_path:
return
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__':
UI_FILE = "uv/main_window.ui"
app = QtWidgets.QApplication(sys.argv)
# 设置应用程序样式
app.setStyle("Fusion")
main = MainWindows()
main.ui.show()
sys.exit(app.exec())