0

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

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()
4

1 回答 1

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()
于 2017-11-17T14:37:35.410 回答