JPG TO PDF(用python将图片转换为pdf格式)

import fpdf
from tkinter import *
import os
from tkinter import filedialog
from tkinter.messagebox import *
from tkinter import ttk
import random
import tkinter as tk


def hp(width,height):
    hp = Tk()
    hp.title("Jpg To Pdf")
    hp.resizable(False,False)#设置窗口不可调节高度和宽度
    w = hp.winfo_screenwidth()#获取屏幕宽度
    h = hp.winfo_screenheight()#获取屏幕高度
    pw = (w - width - 110)/2
    ph = (h - height - 80)/2
    hp.geometry("%dx%d+%d+%d"%(width,height,pw,ph))#设置窗口居中

    def 选择jpg():
        global jpg_file#声明全局变量函数jpg_file
        try:
            file = tk.filedialog.askopenfilename(title = "选择jpg",filetypes = [("JPEG图片格式","*.jpg"),("PNGF图片格式","*.png"),("所有文件","*.*")])#打开文件选择对话框
            jpg_file = os.path.basename(file)#获取选择文件文件名
            os.chdir(os.path.dirname(file))#更改操作目录
            输入框.insert(END,file)
        except UnicodeDecodeError:
            tk.messagebox.showinfo("Error")
            #(打开格式错误)
        finally:
            pass
        

    def 开始转换():
        global jpg_file
        try:
            pdf = fpdf.FPDF("p","mm","A4")
            pdf.add_page()
            pdf.image(jpg_file,0,0,210,210)
            #生成pdf格式
            filename = random.randint(10000000,99999999)#随机命名文件名(防止文件名重复覆盖原文件)
            pdf.output(str(filename) + ".pdf","F")#输出pdf格式
            tk.messagebox.showinfo("Ready","转换完成")
            输入框.delete(0,END)
        except UnicodeEncodeError:
            tk.messagebox.showinfo("Error")
            #(转换格式错误)
        finally:
            pass
        


    标签 = Label(hp,text = "Jpg To Pdf",font = ("微软雅黑",20))
    标签.place(x = 95,y = 10)

    输入框 = ttk.Entry(hp)
    输入框.place(x = 90,y = 80,width = 160,height = 21)

    选择style = ttk.Style()
    选择style.configure("选择.TButton",font = ("微软雅黑",10),fontground = "white")

    选择 = ttk.Button(hp,text = "选择jpg",style = "选择.TButton",command = 选择jpg)
    选择.place(x = 133,y = 130,width = 63,height = 30)

    开始style = ttk.Style()
    开始style.configure("开始.TButton",font = ("微软雅黑",10))
    开始 = ttk.Button(hp,text = "开始转换",style = "开始.TButton",command = 开始转换)
    开始.place(x = 120,y = 200,width = 93,height = 30)
    ##tk组件设置

    hp.mainloop()

if __name__ == "__main__":
    hp(350,300)

你可能感兴趣的:(python,开发语言)