I would like to use PyQt5 to take a screenshot of a webpage. (A full webpage, including the stuff a user wouldn't see unless they scrolled down.)
Supposedly, it is possible to do this in PyQt5 using QtWebEngine. How would you do it though? I specifically don't want the user to see a browser window opening or rendering. I just want a screenshot in a PNG file.
You can take a screenshot of a webpage with the method get_screenshot_as_file() with as parameter the filename.
It is usually found to the right of the “F” (function) keys, toward the top of the keyboard. It can also be found near the directional keys. Click within the web browser to make it your active window. Depending on your keyboard,you may need to press "Alt-Print Screen" to capture the shot.
Please click on the toolbar button (or press Alt+Shift+D combination) to capture the screenshot. You can adjust the screenshot image format from the options page.
Here is an example for QtWebEngine (version 5.12):
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, QUrl, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
class Screenshot(QWebEngineView):
def capture(self, url, output_file):
self.output_file = output_file
self.load(QUrl(url))
self.loadFinished.connect(self.on_loaded)
# Create hidden view without scrollbars
self.setAttribute(Qt.WA_DontShowOnScreen)
self.page().settings().setAttribute(
QWebEngineSettings.ShowScrollBars, False)
self.show()
def on_loaded(self):
size = self.page().contentsSize().toSize()
self.resize(size)
# Wait for resize
QTimer.singleShot(1000, self.take_screenshot)
def take_screenshot(self):
self.grab().save(self.output_file, b'PNG')
self.app.quit()
app = QApplication(sys.argv)
s = Screenshot()
s.app = app
s.capture('https://pypi.org/project/PyQt5/', 'webpage.png')
sys.exit(app.exec_())
-This code was tested in : QT_VERSION_STR = 5.12.1 , PYQT_VERSION_STR = 5.12
NOTE: QtWebKit got deprecated upstream in Qt 5.5 and removed in 5.6.
Instead it is replaced with "QtWebEngineWidgets". So you have to make changes in code.
For more informations: http://doc.qt.io/qt-5/qtwebenginewidgets-qtwebkitportingguide.html
from PyQt5.QtGui import QPainter, QImage
from PyQt5 import QtWebKitWidgets
from functools import partial
class Screenshot(QtWebKitWidgets.QWebView):
def __init__(self):
QtWebKitWidgets.QWebView.__init__(self)
def capture(self, url, output_file):
self.load(QUrl(url))
self.loadFinished.connect(partial(self.onDone, output_file))
def onDone(self,output_file):
# set to webpage size
frame = self.page().mainFrame()
self.page().setViewportSize(frame.contentsSize())
# render image
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
image.save(output_file)
s = Screenshot()
s.capture('https://pypi.org/project/PyQt5/', 'C:/Users/user/Desktop/web_page.png')
result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With