PyQt5使用QWebEngineView进行渲染

时间:2017-11-17 06:56:33

标签: python qt pyqt pyqt5 qwebengineview

我想将网页渲染到屏幕外的图像,但是当我使用下面的代码时,输​​出图像是空白的,我无法解决问题

class PageShotter(QWidget):
    def __init__(self,url,parent=None):
        QWidget.__init__(self,parent)
        self.url = url
    def shot(self):
        self.webview = QtWebEngineWidgets.QWebEngineView(self)
        self.webview.load(QtCore.QUrl(self.url))
        webpage = self.webview.page()
        self.webview.setVisible(True)
        self.webview.loadFinished.connect(self.save)
    def save(self,finished):
        if finished:
            size = self.webview.contentsRect()
            print(u"width:%d,hight:%d" % (size.width(), size.height()))            
            img = QtGui.QImage(size.width(), size.height(), QtGui.QImage.Format_ARGB32)
            painter = QtGui.QPainter(img)            
            self.webview.render(painter,QtCore.QPoint())
            painter.end()
            filename = 'page.png'
            if img.save(filename):
                filepath = os.path.join(os.path.dirname(__file__), filename)
                print(u"success:%s" % filepath)
            else:
                print(u"fail")
        else:
            print("Error")


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    shotter = PageShotter(web_site.base_url)
    shotter.shot()
    app.exec()

1 个答案:

答案 0 :(得分:0)

您必须启用ScreenCaptureEnabled属性,为此必须先启用PluginsEnabled属性。此外,使用show后会呈现内容,但由于您不想显示小部件,我们会启用WA_DontShowOnScreen属性。

import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets

class PageShotter(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, url, parent=None):
        super(PageShotter, self).__init__(parent)
        self.loadFinished.connect(self.save)
        self.url = url
        self.setAttribute(QtCore.Qt.WA_DontShowOnScreen, True)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
        self.show()
        settings = QtWebEngineWidgets.QWebEngineSettings.globalSettings()
        for attr in (QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, 
                     QtWebEngineWidgets.QWebEngineSettings.ScreenCaptureEnabled,):
            settings.setAttribute(attr, True)

    def shot(self):
        if self.size().isNull():
            self.resize(640, 480)
        self.load(QtCore.QUrl.fromLocalFile(self.url))

    @QtCore.pyqtSlot(bool)
    def save(self, finished):
        if finished:
            size = self.contentsRect()
            print(u"width:%d,hight:%d" % (size.width(), size.height()))
            img = QtGui.QImage(size.width(), size.height(), QtGui.QImage.Format_ARGB32)
            painter = QtGui.QPainter(img)
            self.render(painter)
            painter.end()
            filename = 'page.png'
            if img.save(filename):
                filepath = os.path.join(os.path.dirname(__file__), filename)
                print(u"success:%s" % filepath)
            else:
                print(u"fail")
        else:
            print("Error")
        self.close()


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    shotter = PageShotter(r'C:\Output.html')
    shotter.shot()
    app.exec()