View PDF for Python Tkinter

I'm currently looking for the ability to display PDF files inside a Tkinter application (displaying them, for example, as a Frame widget or similar).

Is there a solution to this problem?

I already searched for SO, used ddg others, but found nothing for this purpose. The only thing I found is how to print the contents of the tk.Canvas file to PDF - is there a way to load the PDF into the canvas?

+4
source share
2 answers

Finally, I came up with a solution with which I can work.

Using pypdfocr and its pypdfocr_gs library, I call

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

jpg-, PIL ImageTk.PhotoImage .

ImageTk.PhotoImage(_img_file_handle)

, .

Edit:

,


    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:

viranthas pypdfocr, , ​​ Windows 10 pythons:

# 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
+5

- "python pdf parsing". Google SO pdfminer. , pdfminer , pdfminer. pdfminer Py3 2 & 3.

0

All Articles