记录一下打包命令
pip install pyinstaller
pip install auto-py-to-exe
执行
auto-py-to-exe
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QFileDialog, QHBoxLayout
from pptx import Presentation
from pptx.util import Inches
import os
class FileFolderApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
h1layout = QHBoxLayout()
# 创建布局
layout = QVBoxLayout()
# 创建“选择文件夹”按钮,并连接其点击事件到on_choose_folder方法
self.btn_choose_folder = QPushButton('选择图片所在文件夹')
self.btn_choose_folder.clicked.connect(self.on_choose_folder)
h1layout.addWidget(self.btn_choose_folder)
# 创建标签用于显示文件夹路径
self.label_folder_path = QLabel('未选择文件夹')
h1layout.addWidget(self.label_folder_path)
layout.addLayout(h1layout)
#h2layout = QHBoxLayout()
#self.btn_choose_folder2 = QPushButton('选择输出目录')
#self.btn_choose_folder2.clicked.connect(self.on_choose_folder2)
#h2layout.addWidget(self.btn_choose_folder2)
#self.label_folder_path2 = QLabel('未选择文件夹')
#h2layout.addWidget(self.label_folder_path2)
#layout.addLayout(h2layout)
# 创建“选择文件”按钮,并连接其点击事件到on_choose_file方法(此处仅打印消息,并不实际选择文件)
self.btn_choose_file = QPushButton('执行')
self.btn_choose_file.clicked.connect(self.on_execute)
self.label_result_path = QLabel('')
layout.addWidget(self.label_result_path)
layout.addWidget(self.btn_choose_file)
# 设置窗口标题和大小
self.setWindowTitle('文件夹和文件选择器')
self.setGeometry(300, 300, 300, 200)
self.setLayout(layout)
def on_choose_folder(self):
# 打开文件夹对话框并获取选择的文件夹路径
folder_path = QFileDialog.getExistingDirectory(self, '选择文件夹')
if folder_path:
# 将选择的文件夹路径显示在标签上
self.label_folder_path.setText(folder_path)
self.srcPath = folder_path
def on_choose_folder2(self):
# 打开文件夹对话框并获取选择的文件夹路径
folder_path = QFileDialog.getExistingDirectory(self, '选择文件夹')
if folder_path:
# 将选择的文件夹路径显示在标签上
self.label_folder_path2.setText(folder_path)
self.targetPath = folder_path
def on_execute(self):
# 这里只是打印一句话到控制台,并不实际选择文件
targetFile = self.srcPath+"/hello.pptx"
srcFolder = self.srcPath
print("srcpath:{srcFolder}")
print("targetpath:{targetFile}" )
if self.is_folder_empty(srcFolder):
self.label_result_path.setText(f"目标文件夹为空")
else:
self.create_ppt_from_images(srcFolder,targetFile)
def is_folder_empty(self,folder_path):
return not os.listdir(folder_path)
def create_ppt_from_images(self,image_folder, ppt_filename):
# 创建一个新的PPT文件
prs = Presentation()
# 遍历文件夹中的所有文件
for filename in os.listdir(image_folder):
# 检查文件是否为图片(这里以.jpg和.png为例)
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff','.webp')):
# 构建图片的完整路径
image_path = os.path.join(image_folder, filename)
# 添加一张新的幻灯片
slide = prs.slides.add_slide(prs.slide_layouts[5]) # 使用默认的图片布局(索引5可能因版本而异)
# 将图片添加到幻灯片中
left = top = Inches(0)
pic = slide.shapes.add_picture(image_path, left, top)
# 调整图片大小以填充幻灯片(可选)
pic_width = prs.slide_width
pic_height = int(pic.height * pic_width / pic.width)
#pic_height = int(prs.slide_width)
pic.width = int(pic_width)
pic.height = int(pic_height)
pic.left = 0
pic.top = int((prs.slide_height - pic_height) / 2)
#pic.top = 0
# 保存PPT文件
prs.save(ppt_filename)
self.label_result_path.setText(f"PPT文件已保存为:{ppt_filename}")
print(f"PPT文件已保存为:{ppt_filename}")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FileFolderApp()
ex.show()
sys.exit(app.exec_())