gpt4 book ai didi

python - 如何用python-poppler-qt4显示PDF?

转载 作者:行者123 更新时间:2023-11-28 23:01:49 26 4
gpt4 key购买 nike

我已经下载并安装了python-poppler-qt4我现在正在尝试一个简单的 Qt 应用程序来显示 PDF 页面。我已经按照我能够从网络上获得的内容进行操作,即将 PDF 转换为 QImage,然后转换为 QPixMap,但它不起作用(我得到的只是一个没有可见内容的小窗口)。

我可能在某些时候失败了(QImage.width() 返回我输入的宽度,QPixMap.width() 返回 0)。

代码如下:

#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore
import popplerqt4

class Application(QtGui.QApplication):
def __init__(self):
QtGui.QApplication.__init__(self, sys.argv)
self.main = MainWindow()
self.main.show()

class MainWindow(QtGui.QFrame):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.layout = QtGui.QVBoxLayout()
self.doc = popplerqt4.Poppler.Document.load('/home/benjamin/test.pdf')
self.page = self.doc.page(1)
# here below i entered almost random dpi, position and size, just to test really
self.image = self.page.renderToImage(150, 150, 0, 0, 210, 297)
self.pixmap = QtGui.QPixmap()
self.pixmap.fromImage(self.image)
self.label = QtGui.QLabel(self)
self.label.setPixmap(self.pixmap)
self.layout.addWidget(self.label)
self.setLayout(self.layout)

if __name__ == "__main__":
application = Application()
sys.exit(application.exec_())

这里哪里出错了?谢谢。

最佳答案

我不熟悉 python,所以这可能不直接适用,但是 QPixmap::fromImage是一个返回QPixmap 的静态函数。所以你的代码应该是这样的:

 self.pixmap = QtGui.QPixmap.fromImage(self.image)

换句话说,self.pixmap.fromImage 不会更改 self.pixmap,它会返回一个新的像素图,该像素图是根据您作为参数提供的图像生成的。

关于python - 如何用python-poppler-qt4显示PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10562945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com