[Python] 纯文本查看 复制代码
#coding:utf-8
import tkinter as tk
from tkinter import Label
from tkinter import Entry
from tkinter import filedialog
from tkinter.font import Font
import os
import fitz
from tkinterdnd2 import DND_FILES,TkinterDnD
def center_window(root, width, height): #创建窗口居中
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = int((screen_width - width) / 2)
y = int((screen_height - height) / 2)
root.geometry(f"{width}x{height}+{x}+{y}")
def open_file(): #浏览文件按钮代码
filetypes = (('pdf files', '*.pdf'),('All files', '*.*'))
file = filedialog.askopenfilename(title='选择PDF文件',initialdir=os.getcwd(),filetypes=filetypes)
pathname.delete(0, tk.END)
pathname_2.delete(0, tk.END)
if str.upper(file[-3:])=='PDF':
pathname.insert(0, os.path.split(file)[1])
pathname_2.insert(0, file)
def main(): #开始转换按钮代码
path = pathname_2.get()
name = pathname.get()[:-4]
value_list = [1/3,2/3,4/3,8/3,16/3]
# 图片缩放倍数(0.25,0.5,原分辨率,2倍,4倍)
value_base =int(slider.get())
value = value_list[value_base]
pdf = fitz.open(path)
for page_num in range(len(pdf)):
page = pdf.load_page(page_num)
mat = fitz.Matrix(value,value)
pix = page.get_pixmap(matrix=mat)
# 图片缩放
if len(pdf) == 1:
outpath = path[:-4]+'.jpg'
else:
if not os.path.exists(path[:-4]):
os.mkdir(path[:-4])
outpath = os.path.join(path[:-4] , f'{name}_{page_num}.jpg')
pix.save(outpath)
num = (page_num+1)/len(pdf)*100//1.25
text = '正在转换'+'.'* int(num)+str(round((page_num+1)/len(pdf)*100,1))+'%'
label.config(text=text)
win.update()
def drop(event): #支持拖拽文件
file =event.data
label.config(text='')
pathname.delete(0, tk.END)
pathname_2.delete(0, tk.END)
if ' ' in file:
file = file[1:-1] #文件名有空格时,会生成大括号
if str.upper(file[-3:])=='PDF':
pathname.insert(0, os.path.split(file)[1])
pathname_2.insert(0, file)
win = TkinterDnD.Tk()
win.title('PDF转JPG工具')
center_window(win, 500, 110)
win.resizable(False, False) #锁定窗口大小
win.wm_attributes('-topmost', 1) #窗口保持前置
ft = Font(family='微软雅黑', size=15, weight='bold')
Label(text='选择文件:',font=ft).place(x=40, y=10)
Label(text='转换质量:',font=ft).place(x=40, y=50)
Label(text='低',font=Font(family='微软雅黑', size=12)).place(x=145, y=55)
Label(text='高',font=Font(family='微软雅黑', size=12)).place(x=358, y=55)
entry_font = ('微软雅黑', 12)
pathname = Entry(win, width=28,font=entry_font)
pathname.place(x=150, y=15)
#文本框,用于显示文件名
pathname_2 = Entry(win, width=28,font=entry_font)
pathname_2.place(x=150, y=150)
#隐藏文本框,用于获取完整路径
browser_button = tk.Button(win, text='. . .',font=Font(family='微软雅黑', size=7,weight='bold') ,command=open_file)
browser_button.place(x=420, y=15)
win.drop_target_register(DND_FILES)
win.dnd_bind('<<Drop>>',drop)
label = tk.Label(win,text='', font=Font(family='微软雅黑', size=10))
label.place(x=40, y=85)
#用于显示进度
button = tk.Button(win, text='开始转换',fg='red',font=Font(family='微软雅黑', size=12,weight='bold') ,command=main)
button.place(x=390, y=50)
slider = tk.Scale(win, from_=0, to=4,orient=tk.HORIZONTAL,length=180,sliderlength=30,sliderrelief=tk.RIDGE,showvalue=False,resolution=1)
slider.place(x=170, y=58)
slider.set(2)
win.mainloop()