添加读取excel文件功能

This commit is contained in:
2025-11-20 17:17:15 +08:00
parent d5d92832a8
commit 6de40a4c8a
7 changed files with 194 additions and 5 deletions

35
.gitignore vendored Normal file
View File

@@ -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

28
dynamic_load_widet.py Normal file
View File

@@ -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())

View File

@@ -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")

6
pyproject.toml Normal file
View File

@@ -0,0 +1,6 @@
[project]
name = "excelsplit"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.13"
dependencies = []

43
ui/dynamic_load_widget.ui Normal file
View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>835</width>
<height>492</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QPushButton" name="add_widget_btn">
<property name="geometry">
<rect>
<x>660</x>
<y>120</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>添加控件</string>
</property>
</widget>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>120</x>
<y>100</y>
<width>411</width>
<height>211</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout"/>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>878</width>
<height>432</height>
<height>692</height>
</rect>
</property>
<property name="windowTitle">
@@ -72,6 +72,57 @@
</rect>
</property>
</widget>
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>590</x>
<y>410</y>
<width>69</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>700</x>
<y>410</y>
<width>121</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>生成单个EXCEL文件</string>
</property>
</widget>
<widget class="QScrollArea" name="scrollArea">
<property name="geometry">
<rect>
<x>40</x>
<y>410</y>
<width>181</width>
<height>261</height>
</rect>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>179</width>
<height>259</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2"/>
</item>
</layout>
</widget>
</widget>
</widget>
<resources/>
<connections/>

3
uv.toml Normal file
View File

@@ -0,0 +1,3 @@
[[index]]
url = "https://mirrors.aliyun.com/pypi/simple/"
default = true