使用python3显示PDF文件 [英] Displaying PDF files with python3
问题描述
我想编写一个显示PDF文件的python3/PyGTK3应用程序,但是我找不到一个允许我执行此操作的python软件包.
有 pypoppler ,但它看起来已经过时(?),而且似乎不支持python3(?)>
您有什么建议吗?
请注意,我不需要花哨的功能,例如pdf表单,操作或书写.
事实证明,新版本的poppler-glib不需要这样的绑定.它们与GObject Introspection文件一起提供,因此可以按以下方式导入和使用:
#!/usr/bin/python3
from gi.repository import Poppler
document = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)
print(document.get_pdf_version_string())
那很容易,不是吗?我花了几个小时才发现...
请注意,如果还要导入GTK,则至少需要poppler-0.18.
这是带有GUI的另一个最小示例:
#!/usr/bin/python3
from gi.repository import Poppler, Gtk
def draw(widget, surface):
page.render(surface)
document = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)
page = document.get_page(0)
window = Gtk.Window(title="Hello World")
window.connect("delete-event", Gtk.main_quit)
window.connect("draw", draw)
window.set_app_paintable(True)
window.show_all()
Gtk.main()
I want to write a python3/PyGTK3 application that displays PDF files and I was not able to find a python package that allows me to do that.
There is pypoppler, but it looks outdated (?) and does not seem to support python3 (?)
Do you have any suggestions?
EDIT: Note, that I don't need fancy features, like pdf forms, manipulation or writing.
It turns out, that newer versions of poppler-glib don't require bindings as such. They ship with GObject Introspection files and can therefore be imported and used as follows:
#!/usr/bin/python3
from gi.repository import Poppler
document = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)
print(document.get_pdf_version_string())
That was easy, wasn't it? It took me hours to find that out ...
Note that one needs at least poppler-0.18, if one wants to import GTK as well.
Here is another minimal example with a GUI:
#!/usr/bin/python3
from gi.repository import Poppler, Gtk
def draw(widget, surface):
page.render(surface)
document = Poppler.Document.new_from_file("file:///home/me/some.pdf", None)
page = document.get_page(0)
window = Gtk.Window(title="Hello World")
window.connect("delete-event", Gtk.main_quit)
window.connect("draw", draw)
window.set_app_paintable(True)
window.show_all()
Gtk.main()
这篇关于使用python3显示PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!