PDF Viewer for Python Tkinter

二次信任 提交于 2019-11-30 05:17:23

问题


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?

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?


回答1:


Finally I came to a solution I can work with.

Using pypdfocr and its pypdfocr_gs library I call

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

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.

Edit:

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()

Edit:

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



回答2:


Your search keywords are "python pdf parsing". Google turns up this SO question and pdfminer. There was also this review that settled on pdfminer as the best of a not-great choice, but it is two years older than the latest pdfminer release. There are also pdfminer versions for Py3 and for 2&3.



来源:https://stackoverflow.com/questions/26653929/pdf-viewer-for-python-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!