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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值