應(yīng)用場景 需要將pdf文件轉(zhuǎn)換為圖片薛窥,
打包
pyinstaller -F 拆分pdf文件.py #會顯示控制臺 cmd
pyinstaller -F -w 拆分pdf文件.py #不會顯示控制臺
import fitz
import os
import tkinter as tk
from tkinter import filedialog as fd
import time
import tkinter.messagebox
file_types = [('PDF文件', '.pdf')]
def convert_pdf2img(file_relative_path, pdfinputout):
"""
file_relative_path : 文件相對路徑
"""
print(pdfinputout, 101010101)
page_num = 1
# 獲取最后一個斜杠的索引
lastIndex = pdf_in.get().rfind("/")
print(lastIndex, 8888888)
print(file_relative_path[:lastIndex])
# if not os.path.exists(filename):
# os.makedirs(filename)
pdf = fitz.open(file_relative_path)
print(pdf)
print(pdf)
for page in pdf:
rotate = int(0)
# 每個尺寸的縮放系數(shù)為2浑侥,這將為我們生成分辨率提高4的圖像精偿。
# 此處若是不做設(shè)置丹弱,默認(rèn)圖片大小為:792X612, dpi=96
zoom_x = 2 # (2-->1584x1224)
zoom_y = 2
mat = fitz.Matrix(zoom_x, zoom_y)
pixmap = page.get_pixmap(matrix=mat, alpha=False)
pixmap.pil_save(
fr"{pdfinputout if pdfinputout else file_relative_path[:lastIndex]}\{file_relative_path[lastIndex:len(file_relative_path) - 4]}{page_num}.png")
print(f"第{page_num}保存圖片完成")
page_num = page_num + 1
tkinter.messagebox.showinfo('轉(zhuǎn)換成功', "\n共轉(zhuǎn)換" + f'{len(pdf)}張圖片')
# if __name__ =="__main__":
# # 文件夾中文件名
# file_relative_path = r"C:\Users\osc.luxin.wang\Desktop\新建文件夾\XaP CPK.PDF"
# convert_pdf2img(file_relative_path)
# 選擇文件
def select_out():
# path_save = fd.asksaveasfilename(defaultextension='*.pdf', filetypes=file_types)
# path_save = fd.askdirectory()
path_save = fd.askopenfilename(filetypes=[('pdf', '.pdf')])
print(path_save)
if path_save != '':
button_split['state'] = 'normal'
button_input_out['state'] = 'normal'
pdf_in.set(path_save)
# 選擇保存文件
def select_save():
# path_save = fd.asksaveasfilename(defaultextension='*.pdf', filetypes=file_types)
path_save = fd.askdirectory()
print(path_save)
# a = fd.askdirectory()
if path_save != '':
button_split['state'] = 'normal'
pdf_input_out.set(path_save)
def pdf_split():
# printout = pdf_out.get() + '.pdf'
printin = pdf_in.get().replace('/', "\\") + ""
pdfinputout = pdf_input_out.get().replace('/', "\\") + ""
convert_pdf2img(printin, pdfinputout)
global pdf_in, button_input, button_split, pdf_input_out, button_input_out
def main(root3):
global pdf_in, button_input, button_split, pdf_input_out, button_input_out
pdf_in = tk.StringVar()
# pdf_out = tk.StringVar()
pdf_input_out = tk.StringVar()
label_input = tk.Label(root3, text='①選擇要轉(zhuǎn)換的pdf文件:')
entry_input = tk.Entry(root3, textvariable=pdf_in, width=45)
button_input = tk.Button(root3, text='①選擇文件', command=select_out)
# button_input['state'] = 'disabled'
# page_out = tk.Label(root3, text='②合并pdf文件名:')
# page_out_ = tk.Label(root3, text='(命名格式為‘XXX’)')
# entry_out2 = tk.Entry(root3, textvariable=pdf_out, width=45)
label_input_out = tk.Label(root3, text='②選擇要保存圖片文件夾:')
page_out_out = tk.Label(root3, text='(若不選擇保存文件夾庐船,默認(rèn)保存到pdf所在文件夾’)')
entry_input_out = tk.Entry(root3, textvariable=pdf_input_out, width=45)
button_input_out = tk.Button(root3, text='②選擇文件夾', command=select_save)
button_input_out['state'] = 'disabled'
button_split = tk.Button(root3, text='③執(zhí)行轉(zhuǎn)換', command=pdf_split, width=20, height=3)
button_split['state'] = 'disabled'
label_input.place(x=10, y=30)
entry_input.place(x=10, y=60)
button_input.place(x=350, y=62)
# page_out.place(x=10, y=80)
# page_out_.place(x=10, y=105)
# entry_out2.place(x=10, y=125)
label_input_out.place(x=10, y=155)
page_out_out.place(x=10, y=175)
entry_input_out.place(x=10, y=205)
button_input_out.place(x=350, y=205)
button_split.place(x=220, y=280)
root2 = tk.Tk()
# 窗口尺寸
# root.geometry('400x300')
# 窗口居中
sw = root2.winfo_screenwidth()
sh = root2.winfo_screenheight()
c = (sw - 400) / 2
d = (sh - 300) / 2
# print(a,b,c,d)
root2.geometry('605x500+%d+%d' % (c, d))
# 軟件標(biāo)題
root2.title('PDF文件轉(zhuǎn)換圖片')
# 軟件左上角圖標(biāo)
# root2.iconbitmap('tubiao.ico')
# 窗口大小不可調(diào)
root2.resizable(width=False, height=False)
root = tk.Frame(root2, width=605, height=500)
root.place(x=0, y=0)
main(root)
root2.mainloop()
time.sleep(10)