PYTHON----创建自己的pdf查看器

需求:

做一个小工具,能够快速查看以图片为主的pdf文件,不管多大页面都可以整页显示,如果有多页也能翻页显示。

实现:

定义pdf文件查看类,获取文件名后,自动缩放到指定大小,嵌入前后翻页按钮

class PDFImageViewer:
    
    def __init__(self, pdf_path):
        self.pdf_path = pdf_path
        self.pdf = fitz.open(self.pdf_path)
        # 隐藏根窗口
        self.root=tk.Tk()
        self.root.withdraw()
        self.topwindow = tk.Toplevel()
        self.topwindow.title("PDF文件预览")
        
        self.image_label = tk.Label(self.topwindow)
        
        #self.buttons=tk.Frame(self.root)

        self.current_page = 0
        self.load_page()
 
    def load_page(self):
        page = self.pdf[self.current_page]
        pix = page.get_pixmap()
        img_data = pix.tobytes()
        # 设置新的宽度
        new_height= 1000
        # 计算新的高度
        new_width = int(pix.width * new_height / pix.height)
        img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
        img_resized = img.resize((new_width, new_height), 4)
        img_tk = ImageTk.PhotoImage(image=img_resized)
        self.image_label.config(image=img_tk)
        self.image_label.image = img_tk
        self.root.title(f"PDF Image Viewer - Page {self.current_page + 1} of {len(self.pdf)}")
 
    def next_page(self):
        if self.current_page < len(self.pdf) - 1:
            self.current_page += 1
            self.load_page()
 
    def previous_page(self):
        if self.current_page > 0:
            self.current_page -= 1
            self.load_page()
 
    def run(self):

        
        button_next = tk.Button(self.topwindow, text='Next', command=self.next_page)
        button_previous = tk.Button(self.topwindow, text='Previous', command=self.previous_page)
        
        button_next.grid(row=0,column=0)
        button_previous.grid(row=0,column=1)

        self.image_label.grid(row=1,columnspan=2)
        self.root.mainloop()

打开程序时,先选定文件夹,创建一个列表,将选定文件夹下的所有PDF文件在列表中显示,

通过双击文件名快速全页面预览。

实现代码:

root = tk.Tk()
root.title("File Explorer")

folder_path=select_folder()
sPath=tk.StringVar(value=folder_path) 
listbox_scrollbar = tk.Scrollbar(root)
listbox_width = 60
listbox = tk.Listbox(root, yscrollcommand=listbox_scrollbar.set,width=listbox_width)
 
# 填充文件列表
for file in os.listdir(folder_path):
    extension = os.path.splitext(file)[-1]
    if(extension==".pdf" or extension==".tif" or extension==".jpg"):
        listbox.insert(tk.END, file)
 
# 绑定双击事件
listbox.bind('<Double-Button-1>', on_double_click)
 
listbox.pack(side=tk.LEFT, fill=tk.BOTH,expand=True)
listbox_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
button = tk.Button(root, text="关闭窗口", command=close_window)
button.pack()


root.mainloop()

定义双击事件

def on_double_click(event):
    # 获取选中的文件名
    selected_file = listbox.get(listbox.curselection())
    # 打开文件
    open_file(selected_file)

定义打开文件方法:

def open_file(file_name):
    sPath1=sPath.get()
    # 这里可以添加打开文件的具体逻辑,例如用默认程序打开
    try:
        pdf_viewer=PDFImageViewer(sPath1+"/"+file_name)
        pdf_viewer.run()
    except Exception as e:
        messagebox.showerror("Error", "Unable to open file: " + str(e))

本工具将pdf文件无论是印刷用图,还是普通pdf书籍文件用图片 方式整页显示,达到快速预览效果,尤其适合印刷行业快速印前检查。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值