thedemon
thedemon

Reputation: 337

View a document TKinter

I am new to TKinter and cant seem to find any examples on how to view a document in a window. What I am trying to accomplish is that when selecting a PDF or TIF it will open the file and show the first page in a window using TKinter. Is this possible?

Upvotes: 3

Views: 8327

Answers (4)

Minion Jim
Minion Jim

Reputation: 1279

The following code uses the old library pypdfocr (I had to modify it to work with Python 3) and Ghostscript (Ghostscript needs to be installed outside of Python and so isn't ideal for software to be distributed) to convert each of the PDF's pages into an image that can then be opened by PIL. In the sense that it uses very old libraries and packages, DrD's answer is probably more appropriate.

import glob
import PIL.Image
import PIL.ImageTk
import pypdfocr.pypdfocr_gs as pdfImg

files = glob.glob(pdfImg.PyGs({}).make_img_from_pdf(file_name)[1])
pages = [PIL.ImageTk.PhotoImage(PIL.Image.open(file)) for file in files]

Upvotes: 1

DrD
DrD

Reputation: 439

A long time has passed since this question was posted but, for those still looking for a solution, here's one I've found :

https://github.com/rk700/PyMuPDF/wiki/Demo:-GUI-script-to-display-a-PDF-using-wxPython-or-Tkinter

Basically, it uses PyMuPDF, a python binding for MuPDF. MuPDF is a lightweight document viewer capable of displaying a few file formats, such as pdf and epub.

I quote the code used for TKinter:

This demo can easily be adopted to Tkinter. You need the imports

from Tkinter import Tk, Canvas, Frame, BOTH, NW
from PIL import Image, ImageTk  

and do the following to display each PDF page image:

#-----------------------------------------------------------------
# MuPDF code
#-----------------------------------------------------------------
pix = doc.getPagePixmap(pno - 1)      # create pixmap for a page

#-----------------------------------------------------------------
# Tkinter code
#-----------------------------------------------------------------
self.img = Image.frombytes("RGBA",
                       [pix.width, pix.height],
                       str(pix.samples))
self.photo = ImageTk.PhotoImage(self.img)
canvas = Canvas(self, width=self.img.size[0]+20, 
            height=self.img.size[1]+20)
canvas.create_image(10, 10, anchor=NW, image=self.photo)
canvas.pack(fill=BOTH, expand=1)

Upvotes: 2

jjcyalater
jjcyalater

Reputation: 59

Nothing is impossible my friend!

Try using snippets from this: http://nedbatchelder.com/blog/200712/extracting_jpgs_from_pdfs.html to convert the pdf to an image. I would then display the image on a label using PIL and Tkinter.

And I think Tif files should display on a label without problems IIRC.

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 385780

No, it is not possible to show a TIF or PDF in a Tkinter window. Your main options are to show plain text, and to show images (.gif with plain Tkinter, other formats if you include PIL - the python imaging library).

Upvotes: 1

Related Questions