Search code examples
pythonpyqtpyqt5python-3.6qwebengineview

save html files in QWebEngineView browser


I am trying to create my own browser using Python QWebEngineView . I have followed a tutorial that worked on an previous version of PyQt5 (around 2015), but due to its recent updates, some parts of the previous code no longer work.

I have fixed most errors but I am unable to perform html file opening/saving. I always receive a system error when I click on the save button. The following is my code for file saving:

(QMainWindow class)

save_file_action = QAction(QIcon("disk--pencil.png"), "Save Page As...", self)
save_file_action.setStatusTip("Save current page to file")
file_menu.addAction(save_file_action)

(save_file function)

def save_file(self):
    filename, _ = QFileDialog.getSaveFilename(self, "Save Page As", "",
        "Hypertext Markup Language (*.htm *.html);;"    
        "All files(*.*)")

    if filename:
        html = self.browser.page().mainFrame().toHtml()
        with open(filename, 'w') as f:
            f.write(html)

Thank you.


Solution

  • the toHtml() function of QtWebEngine is asynchronous, so it does not return anything directly, but you have to pass it a callback so that in that function returns the html, to convert that process asynchronous to synchronous we use a QEventLoop with the help of a signal :

    import sys
    
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtWebEngineWidgets import *
    
    class Browser(QMainWindow):
        htmlFinished = pyqtSignal()
        def __init__(self, *args, **kwargs):
            QMainWindow.__init__(self, *args, **kwargs)
            self.mHtml = ""
            self.view = QWebEngineView()
            self.setCentralWidget(self.view)
            self.view.setUrl(QUrl("http://www.google.com/"))
            file_menu = QMenu(self.menuBar())
            file_menu.setTitle("File")
            save_file_action = QAction(QIcon("disk--pencil.png"), "Save Page As...",self)
            file_menu.addAction(save_file_action)
            self.menuBar().addAction(file_menu.menuAction())
            save_file_action.triggered.connect(self.save_file)
    
        def callback(self, html):
            self.mHtml = html
            self.htmlFinished.emit()
    
        def save_file(self):
            filename, _ = QFileDialog.getSaveFileName(self, "Save Page As", "", "Hypertext Markup Language (*.htm *.html);;" "All files(*.*)")
            if filename:
                self.view.page().toHtml(self.callback)
                loop = QEventLoop()
                self.htmlFinished.connect(loop.quit)
                loop.exec_()
                with open(filename, 'w') as f:
                    f.write(self.mHtml)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = Browser()
        w.show()
        sys.exit(app.exec_())