python-pptx
python-pptx copied to clipboard
Failed to run after packaging
I am trying to package the program; but the packaging is successful but the operation fails.
The procedure is as follows
import os
from tkinter import Tk, Label, Button, filedialog
from pptx import Presentation
from PIL import Image
class PPTCreator:
def __init__(self, root):
self.root = root
self.root.title("PPT Creator")
self.folder_path = None
self.label = Label(self.root, text="请选择包含图片的文件夹")
self.label.pack()
self.select_button = Button(self.root, text="选择文件夹", command=self.select_folder)
self.select_button.pack()
def select_folder(self):
self.folder_path = filedialog.askdirectory()
if self.folder_path:
self.create_ppt()
def create_ppt(self):
presentation = Presentation()
image_files = sorted([f for f in os.listdir(self.folder_path) if f.endswith('.jpg') or f.endswith('.png')])
for image_file in image_files:
slide = presentation.slides.add_slide(presentation.slide_layouts[5])
img_path = os.path.join(self.folder_path, image_file)
img = Image.open(img_path)
width, height = img.size
# 计算缩放比例,使图片适应幻灯片尺寸
max_width = presentation.slide_width
max_height = presentation.slide_height
scale = min(max_width / width, max_height / height)
new_width = int(width * scale)
new_height = int(height * scale)
# 插入图片
slide.shapes.add_picture(img_path, 0, 0, width=new_width, height=new_height)
ppt_filename = os.path.join(self.folder_path, 'output.pptx')
presentation.save(ppt_filename)
self.label.config(text=f"已创建PPT文件:{ppt_filename}")
if __name__ == "__main__":
root = Tk()
ppt_creator = PPTCreator(root)
root.mainloop()