适用于 Python Tkinter 的 PDF 查看器 [英] PDF Viewer for Python Tkinter
问题描述
我目前正在寻找在 Tkinter 应用程序中显示 PDF 文件的可能性(例如在框架小部件或类似工具中显示它们).
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 !!!!
我目前正在更新 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(" mp est.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屋!