![](/img/trans.png)
[英]How to make zoom in PDF viewer with Python tkinter and tkPDFViewer?
[英]PDF Viewer for Python Tkinter
我目前正在寻找在 Tkinter 应用程序中显示 PDF 文件的可能性(例如在 Frame 小部件或类似工具中显示它们)。
这个问题已经有解决方案了吗?
我已经搜索过,使用 ddg 和其他人,但没有找到任何用于此目的的东西。 我唯一发现的是如何将 tk.Canvas 的内容打印为 PDF - 有没有办法将 PDF 加载到 Canvas 中?
!!! 注意力 !!!
!!! 这只适用于 Python 2 !!!!
viranthas pypdfocr 在 python 3 中不能正常工作。
要与 python 2 一起使用,请愉快地使用下面的版本。
最后我找到了一个我可以使用的解决方案。
使用 pypdfocr 及其我调用的 pypdfocr_gs 库
pypdfocr.pypdfocr_gs.PyGs({}).make_img_from_pdf(pdf_file)
检索 jpg 图像,然后我使用 PIL 从中获取 ImageTk.PhotoImage 实例并在我的代码中使用它们。
ImageTk.PhotoImage(_img_file_handle)
我会尽快添加一个适当的例子。
编辑:
正如承诺的那样,代码来了
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()
编辑:
使用 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
2021,一种在 Windows 10 上对我有用的方式。 来自
pip install tkPDFViewer
然后
# 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.