📜  使用 Tkinter – Python将图像转换为 PDF

📅  最后修改于: 2022-05-13 01:54:36.696000             🧑  作者: Mango

使用 Tkinter – Python将图像转换为 PDF

先决条件: Tkinter、img2pdf

Python为开发 GUI(图形用户界面)提供了多种选择。在所有 GUI 方法中,Tkinter 是最常用的方法。它是Python附带的 Tk GUI 工具包的标准Python接口。 Python with Tkinter 是创建 GUI 应用程序的最快、最简单的方法。在本文中,我们将学习如何使用Python中的 Tkinter 将多个图像转换为多个 PDF 或将多个图像转换为一个 PDF。对于转换,我们将使用img2pdf模块。

img2pdf是一个开源的Python包,用于将图像转换为 pdf 格式。它包含另一个模块 Pillow,它也可用于增强图像(亮度、对比度和其他东西)

对于安装,将此命令运行到您的终端中:

pip install img2pdf

让我们一步一步地理解实现:

第 1 步:创建一个 Tkinter 窗口并添加按钮、标签等...

Python3
# Import Module
from tkinter import *
  
# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')
  
  
# Add Labels and Buttons
Label(root, text = "IMAGE CONVERSION", 
      font = "italic 15 bold").pack(pady = 10)
  
Button(root, text = "Select Images", font = 14).pack(pady = 10)
  
frame = Frame()
frame.pack(pady = 20)
  
Button(frame, text = "Image to PDF", relief = "solid",
                   bg = "white", font = 15).pack(side = LEFT,padx = 10)
  
Button(frame, text = "Images to PDF", relief = "solid",
                 bg = "white",font = 15).pack()
  
# Execute Tkinter
root.mainloop()


Python3
def select_file():
    global file_names
    file_names = askopenfilenames(initialdir = "/",title = "Select File")
  
# IMAGE TO PDF
def image_to_pdf():
    for index, file_name in enumerate(file_names):
        with open(f"file {index}.pdf", "wb") as f:
            f.write(img2pdf.convert(file_name))
  
# IMAGES TO PDF
def images_to_pdf():
    with open(f"file.pdf","wb") as f:
        f.write(img2pdf.convert(file_names))


Python3
# Import Module
from tkinter import *
from tkinter.filedialog import askopenfilenames
import img2pdf
  
# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')
  
def select_file():
    global file_names
    file_names = askopenfilenames(initialdir = "/",
                                  title = "Select File")
  
# IMAGE TO PDF
def image_to_pdf():
    for index, file_name in enumerate(file_names):
        with open(f"file {index}.pdf", "wb") as f:
            f.write(img2pdf.convert(file_name))
  
# IMAGES TO PDF
def images_to_pdf():
    with open(f"file.pdf", "wb") as f:
        f.write(img2pdf.convert(file_names))
  
# Add Labels and Buttons
Label(root, text = "IMAGE CONVERSION",
      font = "italic 15 bold").pack(pady = 10)
  
Button(root, text = "Select Images",
       command = select_file, font = 14).pack(pady = 10)
  
frame = Frame()
frame.pack(pady = 20)
  
Button(frame, text = "Image to PDF",
       command = image_to_pdf,
       relief = "solid",
       bg = "white", font = 15).pack(side = LEFT, padx = 10)
  
Button(frame, text = "Images to PDF",
       command = images_to_pdf, relief = "solid",
       bg = "white", font = 15).pack()
  
# Execute Tkinter
root.mainloop()


第 2 步:我们将创建三个函数; select_file() , image_to_pdf( ) , images_to_pdf()

  • select_file:它会帮助您选择文件
  • image_to_pdf:用于将一个图像文件转换为一个pdf文件
  • images_to_pdf:用于将多个图像文件转换为一个pdf文件

蟒蛇3

def select_file():
    global file_names
    file_names = askopenfilenames(initialdir = "/",title = "Select File")
  
# IMAGE TO PDF
def image_to_pdf():
    for index, file_name in enumerate(file_names):
        with open(f"file {index}.pdf", "wb") as f:
            f.write(img2pdf.convert(file_name))
  
# IMAGES TO PDF
def images_to_pdf():
    with open(f"file.pdf","wb") as f:
        f.write(img2pdf.convert(file_names))

下面是实现:

蟒蛇3

# Import Module
from tkinter import *
from tkinter.filedialog import askopenfilenames
import img2pdf
  
# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')
  
def select_file():
    global file_names
    file_names = askopenfilenames(initialdir = "/",
                                  title = "Select File")
  
# IMAGE TO PDF
def image_to_pdf():
    for index, file_name in enumerate(file_names):
        with open(f"file {index}.pdf", "wb") as f:
            f.write(img2pdf.convert(file_name))
  
# IMAGES TO PDF
def images_to_pdf():
    with open(f"file.pdf", "wb") as f:
        f.write(img2pdf.convert(file_names))
  
# Add Labels and Buttons
Label(root, text = "IMAGE CONVERSION",
      font = "italic 15 bold").pack(pady = 10)
  
Button(root, text = "Select Images",
       command = select_file, font = 14).pack(pady = 10)
  
frame = Frame()
frame.pack(pady = 20)
  
Button(frame, text = "Image to PDF",
       command = image_to_pdf,
       relief = "solid",
       bg = "white", font = 15).pack(side = LEFT, padx = 10)
  
Button(frame, text = "Images to PDF",
       command = images_to_pdf, relief = "solid",
       bg = "white", font = 15).pack()
  
# Execute Tkinter
root.mainloop()

输出: