Wednesday, 15 November 2017

Qt5 / QWebKit, an script to show a webpage in a widget and save it to .jpg

# This script could be launched with -profile offscreen to avoid using a desktop; but it failed due to missing fonts


import sys
from PyQt5 import Qt,QtWebKitWidgets

SIZE = 1440,720
URL = 'https://.../Reports/wr/msGUI-js.html'
FILE = '/tmp/test.jpg'
SHOW = True

if not SHOW: ops = sys.argv+['-platform','offscreen']
else: ops = sys.argv

qapp = Qt.QApplication(ops)
    
qwv = QtWebKitWidgets.QWebView()
qwv.load(Qt.QUrl(URL))

qwv.resize(*SIZE)
if SHOW: qwv.show()

def grabber():
    pm = qwv.grab()
    pm.save(FILE,'jpg')
    print('%s done'%FILE)

timer = Qt.QTimer()
timer.timeout.connect(grabber)
timer.start(5000.)

print('starting ...')
sys.exit(qapp.exec_())

No comments:

Post a Comment