I am currently looking for a possibility to display PDF Files inside a Tkinter application (displaying them eg 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?
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.
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.
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()
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
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 .
2021, a way that worked for me on windows 10. From
pip install tkPDFViewer
Then
# Importing tkinter to make gui in python
import os
from tkinter import *
# Importing tkPDFViewer to place pdf file in gui.
# In tkPDFViewer library there is
# an tkPDFViewer module. That I have imported as pdf
from tkPDFViewer import tkPDFViewer as pdf
# Initializing tk
root = Tk()
# Set the width and height of our root window.
root.geometry("550x750")
# creating object of ShowPdf from tkPDFViewer.
v1 = pdf.ShowPdf()
# Adding pdf location and width and height.
v2 = v1.pdf_view(root,
pdf_location=r"C:\repositories\dg_ml\dg_ml_models\deliverynote\deliverynote\visualize\tmp\annotated\243712_637477949668712907_Scan2021-02-01_174914.pdf",
width=50, height=100)
# Placing Pdf in my gui.
v2.pack()
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.