适用于Python Tkinter的PDF查看器 [英] PDF Viewer for Python Tkinter

查看:305
本文介绍了适用于Python Tkinter的PDF查看器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找一种在Tkinter应用程序中显示PDF文件的可能性(例如在Frame小部件或类似窗口中显示它们).

I am currently looking for a possibility to display PDF Files inside a Tkinter application (displaying them e.g. in a Frame widget or similar).

是否已经有解决此问题的方法?

Is there already a solution for this problem?

我已经搜索过SO,使用了ddg,但是没有找到任何用于此目的的东西.我唯一发现的是如何将tk的内容打印到Canvas到PDF-有没有办法将PDF加载到Canvas中?

I already searched SO, used ddg an others but did not find anything for that purpose. Only thing I found was how to print the contents of a tk.Canvas to PDF - is there a way to load a PDF into a Canvas?

推荐答案

!!!注意!!!
!!!这仅适用于PYTHON 2 !!!!

!!! ATTENTION !!!
!!! THIS ONLY WORKS FOR PYTHON 2 !!!!

我目前正在为python3进行更新

viranthas pypdfocr在python 3中无法正常工作.
要与python 2一起使用,请愉快地使用以下版本.

viranthas pypdfocr is not working properly with python 3.
For use with python 2, happily use the version below.

最后,我找到了可以使用的解决方案.

Finally I came to a solution I can work with.

使用pypdfocr及其我称为的pypdfocr_gs库

Using pypdfocr and its pypdfocr_gs library I call

pypdfocr.pypdfocr_gs.PyGs({}).make_img_from_pdf(pdf_file)

检索jpg图片,然后使用PIL从中获取ImageTk.PhotoImage实例,并在我的代码中使用它们.

to retrieve jpg images and then I use PIL to get ImageTk.PhotoImage instances from it and use them in my code.

ImageTk.PhotoImage(_img_file_handle)

我会尽快添加一个适当的示例.

Will add a proper example as soon as I can.

正如这里所承诺的那样,代码是

As promised here comes the code


    import pypdfocr.pypdfocr_gs as pdfImg
    from PIL import Image, ImageTk
    import Tkinter as tk
    import ttk

    import glob, os

    root=tk.Tk()

    __f_tmp=glob.glob(pdfImg.PyGs({}).make_img_from_pdf("\tmp\test.pdf")[1])[0]
    #                             ^ this is needed for a "default"-Config
    __img=Image.open(__f_tmp)

    __tk_img=ImageTk.PhotoImage(__img)

    ttk.Label(root, image=__tk_img).grid()

    __img.close()
    os.remove(__f_tmp)

    root.mainloop()

使用viranthas的pypdfocr版本,Windows 10和pythons子进程的处理中似乎存在一个错误:

Using viranthas pypdfocr version there seems to be a bug inside the handling of Windows 10 and pythons subprocess:

# extract from pypdfocr_gs:
def _run_gs(self, options, output_filename, pdf_filename):
        try:
            cmd = '%s -q -dNOPAUSE %s -sOutputFile="%s" "%s" -c quit' % (self.binary, options, output_filename, pdf_filename)

            logging.info(cmd)        

            # Change this line for Windows 10:
            # out = subprocess.check_output(cmd, shell=True)
            out = subprocess.check_output(cmd)
# end of extract

这篇关于适用于Python Tkinter的PDF查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆