应用场景 需要将pdf文件转换为图片,
打包
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)
# 每个尺寸的缩放系数为2,这将为我们生成分辨率提高4的图像。
# 此处若是不做设置,默认图片大小为: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('转换成功', "\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='①选择要转换的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='(若不选择保存文件夹,默认保存到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='③执行转换', 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))
# 软件标题
root2.title('PDF文件转换图片')
# 软件左上角图标
# root2.iconbitmap('tubiao.ico')
# 窗口大小不可调
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)