0

I tried using the following code to convert webpage to pdf file:

import sys
from PyQt5 import QtWebEngineWidgets, QtCore, QtWidgets

app = QtWidgets.QApplication(sys.argv)
loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)
loader.page().pdfPrintingFinished.connect(
    lambda *args: print('finished:', args))
loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))

def emit_pdf(finished):
    loader.show()
    loader.page().printToPdf("test.pdf")

loader.loadFinished.connect(emit_pdf)

app.exec()

But I received the error below:

Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py", line 2, in <module>
    from PyQt5 import QtWebEngineWidgets, QtCore, QtWidgets
ImportError: cannot import name 'QtWebEngineWidgets' from 'PyQt5' (/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.8/site-packages/PyQt5/__init__.py)
Paul
  • 1,801
  • 1
  • 12
  • 18
  • Does this answer your question? [Cannot import QtWebKitWidgets in PyQt5](https://stackoverflow.com/questions/37876987/cannot-import-qtwebkitwidgets-in-pyqt5) – TZHX Jan 05 '22 at 10:20
  • Does this answer your question? [Python 3.7.0 No module named 'PyQt5.QtWebEngineWidgets'](https://stackoverflow.com/questions/51154871/python-3-7-0-no-module-named-pyqt5-qtwebenginewidgets) – musicamante Jan 05 '22 at 13:28
  • Depending on the distribution, QtWebEngineWidgets are not installed by default when installing PyQt, since it's on a separated module that is normally not required for standard usage. If you installed PyQt with pip, `pip install PyQtWebEngine`. – musicamante Jan 05 '22 at 13:28

1 Answers1

0

You may try running it on Python 3.7, if you are using a version later than that. (I've been having this exact error despite following the "fixes" in other posts, but once I used Python 3.7 and PyQt5==5.15.0 my application ran without issue.)

elunomas
  • 161
  • 6