Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to run after packaging #959

Open
mayjack0312 opened this issue Mar 17, 2024 · 0 comments
Open

Failed to run after packaging #959

mayjack0312 opened this issue Mar 17, 2024 · 0 comments

Comments

@mayjack0312
Copy link

I am trying to package the program; but the packaging is successful but the operation fails.
69105624b9f74cf1347fbaa1692c389

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant