ใช้โมดูล img2pdf แปลงไฟล์ภาพเป็น PDF ร่วมกับ Tkinter สร้าง GUI เลือกไฟล์หลายไฟล์
ขั้นตอน
- ติดตั้ง:
pip install img2pdf - ใช้ filedialog.askopenfilenames() เลือกหลายไฟล์
- อ่านไฟล์เป็น Binary และแปลงด้วย img2pdf.convert()
- บันทึกเป็น .pdf
ตัวอย่างโค้ด
# Import required Libraries
from tkinter import *
from tkinter import filedialog
import img2pdf
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
win.geometry('750x250')
win.title("Image to PDF")
def select_file():
global images
images = filedialog.askopenfilenames(initialdir="", title="Select Images")
Label(win, text=images).pack()
def image_to_pdf():
for image in enumerate(images):
with open(f"{image}.pdf", "wb") as file:
file.write(img2pdf.convert(images))
Label(win, text=file).pack()
# Add Labels and Buttons
Label(win, text="Image to PDF Convertor", font="Caveat 25 bold").pack(pady=30)
ttk.Button(win, text="Select Images", command=select_file).pack(ipadx=10)
frame = Frame(win)
frame.pack()
ttk.Button(frame, text="Convert and Save", command=image_to_pdf).pack(side=LEFT, pady=20, ipadx=10)
win.mainloop()
ผลลัพธ์
แอปพลิเคชันเลือกภาพและแปลงเป็น PDF
