[Python] How to PyInstaller a non-pure Python library?

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use PyInstaller to package my Python application that uses PyMuPDF (fitz) to convert PDF pages into images and display them in a Tkinter UI. The script works perfectly when run normally with Python, but when I build it with PyInstaller, it fails to PyInstaller.

    I know PyMuPDF is not a pure Python library, and I'm not sure what extra steps I need to take to get PyInstaller to include all the necessary components (DLLs, fonts, resources, etc.).

    How do I properly package this application with PyInstaller so that it works like the original script?

    import fitz
    import os
    import tkinter as tk
    from tkinter import filedialog
    from PIL import Image, ImageTk

    class PDFViewerApp:
    def __init__(self, root):
    self.root = root
    self.root.title("PDF to Image Viewer")

    self.label = tk.Label(root)
    self.label.pack()

    self.btn_frame = tk.Frame(root)
    self.btn_frame.pack()

    self.prev_btn = tk.Button(self.btn_frame, text="← Prev", command=self.show_prev)
    self.prev_btn.grid(row=0, column=0)

    self.next_btn = tk.Button(self.btn_frame, text="Next →", command=self.show_next)
    self.next_btn.grid(row=0, column=1)

    self.load_btn = tk.Button(root, text="select file PDF", command=self.load_pdf)
    self.load_btn.pack(pady=10)

    self.image_paths = []
    self.current_index = 0

    def load_pdf(self):
    file_path = filedialog.askopenfilename(filetypes=[("PDF files", "*.pdf")])
    if file_path:
    output_folder = "output_images"
    self.convert_pdf_to_images(file_path, output_folder)
    self.image_paths = sorted([
    os.path.join(output_folder, f) for f in os.listdir(output_folder)
    if f.endswith(".png")
    ])
    self.current_index = 0
    self.show_image()

    def convert_pdf_to_images(self, pdf_path, output_folder, dpi=150):
    os.makedirs(output_folder, exist_ok=True)
    doc = fitz.open(pdf_path)
    for page in doc:
    pix = page.get_pixmap(dpi=dpi)
    fname = os.path.join(output_folder, f"page_{page.number+1}.png")
    pix.save(fname)

    def show_image(self):
    if not self.image_paths:
    return
    img = Image.open(self.image_paths[self.current_index])
    img = img.resize((600, 800))
    photo = ImageTk.PhotoImage(img)
    self.label.config(image=photo)
    self.label.image = photo

    def show_next(self):
    if self.current_index < len(self.image_paths) - 1:
    self.current_index += 1
    self.show_image()

    def show_prev(self):
    if self.current_index > 0:
    self.current_index -= 1
    self.show_image()

    # Chạy ứng dụng
    if __name__ == "__main__":
    root = tk.Tk()
    app = PDFViewerApp(root)
    root.mainloop()




    I tried to create an .exe file from my Python script using this command:

    bash: pyinstaller --onefile --windowed my_script.py

    I expected it to create an .exe file that runs the GUI application and opens the PDF file, converts each page to images and displays them.

    But PyInstaller can't build the executable. The build takes a very long time and fails

    I also tried using --collect-all fitz and --hidden-import, but still no luck

    Continue reading...

Compartilhe esta Página