From 6de40a4c8a99e0e023a5ed48b2c1121b77d5ba36 Mon Sep 17 00:00:00 2001 From: bd6oc Date: Thu, 20 Nov 2025 17:17:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=BB=E5=8F=96excel?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 35 ++++++++++++++++++++++++++ dynamic_load_widet.py | 28 +++++++++++++++++++++ excel_split.py | 31 ++++++++++++++++++++--- pyproject.toml | 6 +++++ ui/dynamic_load_widget.ui | 43 +++++++++++++++++++++++++++++++ ui/main_window.ui | 53 ++++++++++++++++++++++++++++++++++++++- uv.toml | 3 +++ 7 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 dynamic_load_widet.py create mode 100644 pyproject.toml create mode 100644 ui/dynamic_load_widget.ui create mode 100644 uv.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fafc12e --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# --------------- 代码 --------------- +# 如果想把某目录下所有.py都放进去,就写 *.py 或 src/ + +# --------------- Python 环境 -------- +__pycache__/ +*.pyc +*.pyo +*.pyd +*.so +*.egg +*.egg-info/ +venv/ +env/ +.venv/ + Pipfile.lock +poetry.lock + +# --------------- IDE / 编辑器 -------- +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# --------------- Jupyter / 数据 ------- +.ipynb_checkpoints/ +*.ipynb +data/ +*.csv +*.xlsx +*.json + +# --------------- 系统文件 ------------- +.DS_Store +Thumbs.db \ No newline at end of file diff --git a/dynamic_load_widet.py b/dynamic_load_widet.py new file mode 100644 index 0000000..e2eab5c --- /dev/null +++ b/dynamic_load_widet.py @@ -0,0 +1,28 @@ +import sys + +from PySide6.QtUiTools import QUiLoader +from PySide6.QtWidgets import QApplication, QLabel + + +class dynamic_load_widget(): + def __init__(self): + super(dynamic_load_widget, self).__init__() + + # 显示主窗口 + self.ui = QUiLoader().load('ui/dynamic_load_widget.ui') + + self.ui.add_widget_btn.clicked.connect(self.add_widget) + + self.counter = 0 + + def add_widget(self): + print('add widget') + self.counter += 1 + label = QLabel(f'我是第{self.counter}个动态label') + self.ui.verticalLayout.addWidget(label) + +if __name__ == '__main__': + app = QApplication(sys.argv) + main = dynamic_load_widget() + main.ui.show() + sys.exit(app.exec()) \ No newline at end of file diff --git a/excel_split.py b/excel_split.py index 1453c47..fb2e282 100644 --- a/excel_split.py +++ b/excel_split.py @@ -1,8 +1,10 @@ import sys from PySide6 import QtCore, QtGui, QtWidgets +from PySide6.QtGui import QStandardItemModel, QStandardItem from PySide6.QtUiTools import QUiLoader from pathlib import Path -from PySide6.QtWidgets import QFileDialog, QMainWindow, QMessageBox, QLabel +from PySide6.QtWidgets import QFileDialog, QMainWindow, QMessageBox, QLabel, QComboBox, QVBoxLayout, QSizePolicy, \ + QTableWidgetItem import pandas as pd class MainWindows(QMainWindow): @@ -34,18 +36,39 @@ class MainWindows(QMainWindow): #使用pandas读取EXCEL文件 try: # 读取第一行表头 - df = pd.read_excel(file_path, nrows=0) # 不加载数据,只加载列名 + df = pd.read_excel(file_path, nrows=10) # 不加载数据,只加载列名 headers = list(df.columns) except Exception as e: QMessageBox.critical(self,"错误",f"读取文件失败:\n{e}") return + + # 2. 建 model + model = QStandardItemModel(self) + model.setRowCount(df.shape[0]) + model.setColumnCount(df.shape[1]) + model.setHorizontalHeaderLabels(df.columns) + # 3. 填单元格 + for r, row in df.iterrows(): + for c, val in enumerate(row): + model.setItem(r, c, QStandardItem(str(val))) + + # 4. 挂到视图 + self.ui.tableView.setModel(model) + + + #动态创建标签 + print(headers) for col in headers: + # 生成每个LABEL标签的内容 lbl = QLabel(f"· {col}") - self.ui.addWidget() + # 在窗体中的垂直布局内放置标签 + self.ui.verticalLayout_2.addWidget(lbl) + + self.ui.comboBox.addItems(headers) + if __name__ == '__main__': - UI_FILE = "uv/main_window.ui" app = QtWidgets.QApplication(sys.argv) # 设置应用程序样式 app.setStyle("Fusion") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..4f19a39 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,6 @@ +[project] +name = "excelsplit" +version = "0.1.0" +description = "Add your description here" +requires-python = ">=3.13" +dependencies = [] diff --git a/ui/dynamic_load_widget.ui b/ui/dynamic_load_widget.ui new file mode 100644 index 0000000..3c43aae --- /dev/null +++ b/ui/dynamic_load_widget.ui @@ -0,0 +1,43 @@ + + + Form + + + + 0 + 0 + 835 + 492 + + + + Form + + + + + 660 + 120 + 75 + 23 + + + + 添加控件 + + + + + + 120 + 100 + 411 + 211 + + + + + + + + diff --git a/ui/main_window.ui b/ui/main_window.ui index 448e7fb..1a5e0e7 100644 --- a/ui/main_window.ui +++ b/ui/main_window.ui @@ -7,7 +7,7 @@ 0 0 878 - 432 + 692 @@ -72,6 +72,57 @@ + + + + 590 + 410 + 69 + 22 + + + + + + + 700 + 410 + 121 + 23 + + + + 生成单个EXCEL文件 + + + + + + 40 + 410 + 181 + 261 + + + + true + + + + + 0 + 0 + 179 + 259 + + + + + + + + + diff --git a/uv.toml b/uv.toml new file mode 100644 index 0000000..bff1d6b --- /dev/null +++ b/uv.toml @@ -0,0 +1,3 @@ +[[index]] +url = "https://mirrors.aliyun.com/pypi/simple/" +default = true \ No newline at end of file