#58 网页整体截图
This commit is contained in:
parent
ea89fff811
commit
d2edf9cf05
6 changed files with 84 additions and 1 deletions
|
@ -61,6 +61,7 @@ encoding//QWebEngineView/JsSignals.py=utf-8
|
|||
encoding//QWebView/DreamTree.py=utf-8
|
||||
encoding//QWebView/GetCookie.py=utf-8
|
||||
encoding//QWebView/JsSignals.py=utf-8
|
||||
encoding//QWebView/ScreenShotPage.py=utf-8
|
||||
encoding//QWidget/Lib/CustomPaintWidget.py=utf-8
|
||||
encoding//QWidget/Lib/CustomWidget.py=utf-8
|
||||
encoding//QWidget/WidgetStyle.py=utf-8
|
||||
|
|
BIN
QWebView/Data/ScreenShotPage.png
Normal file
BIN
QWebView/Data/ScreenShotPage.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 312 KiB |
|
@ -4,6 +4,7 @@
|
|||
- [梦幻树](#1梦幻树)
|
||||
- [获取Cookie](#2获取Cookie)
|
||||
- [和Js交互操作](#3和Js交互操作)
|
||||
- [网页整体截图](#4网页整体截图)
|
||||
|
||||
## 1、梦幻树
|
||||
[运行 DreamTree.py](DreamTree.py)
|
||||
|
@ -28,3 +29,11 @@
|
|||
具体看代码中的注释
|
||||
|
||||
![JsSignals](ScreenShot/JsSignals.gif)
|
||||
|
||||
## 4、网页整体截图
|
||||
[运行 ScreenShotPage.py](ScreenShotPage.py)
|
||||
|
||||
原理是通过`QWebView.QWebPage.QWebFrame`得到内容的高度,然后设置`QWebPage.setViewportSize`的大小,
|
||||
最后通过`QWebFrame.render`把图片截出来
|
||||
|
||||
![ScreenShotPage](ScreenShot/ScreenShotPage.png)
|
BIN
QWebView/ScreenShot/ScreenShotPage.png
Normal file
BIN
QWebView/ScreenShot/ScreenShotPage.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 82 KiB |
72
QWebView/ScreenShotPage.py
Normal file
72
QWebView/ScreenShotPage.py
Normal file
|
@ -0,0 +1,72 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Created on 2019年7月8日
|
||||
@author: Irony
|
||||
@site: https://pyqt5.com https://github.com/PyQt5
|
||||
@email: 892768447@qq.com
|
||||
@file: ScreenShotPage
|
||||
@description: 网页整体截图
|
||||
"""
|
||||
import cgitb
|
||||
import os
|
||||
import sys
|
||||
|
||||
from PyQt5.QtCore import QUrl, Qt
|
||||
from PyQt5.QtGui import QImage, QPainter
|
||||
from PyQt5.QtWebKitWidgets import QWebView
|
||||
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QPushButton,\
|
||||
QMessageBox
|
||||
|
||||
|
||||
__Author__ = "Irony"
|
||||
__Copyright__ = "Copyright (c) 2019"
|
||||
__Version__ = "Version 1.0"
|
||||
|
||||
|
||||
class Window(QWidget):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Window, self).__init__(*args, **kwargs)
|
||||
self.resize(600, 400)
|
||||
layout = QVBoxLayout(self)
|
||||
self.webView = QWebView()
|
||||
layout.addWidget(self.webView)
|
||||
layout.addWidget(QPushButton('截图', self, clicked=self.onScreenShot))
|
||||
self.webView.load(QUrl("https://pyqt5.com"))
|
||||
|
||||
def onScreenShot(self):
|
||||
page = self.webView.page()
|
||||
frame = page.mainFrame()
|
||||
size = frame.contentsSize()
|
||||
image = QImage(size, QImage.Format_ARGB32_Premultiplied)
|
||||
image.fill(Qt.transparent)
|
||||
|
||||
painter = QPainter()
|
||||
painter.begin(image)
|
||||
painter.setRenderHint(QPainter.Antialiasing, True)
|
||||
painter.setRenderHint(QPainter.TextAntialiasing, True)
|
||||
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
|
||||
|
||||
# 记录旧大小
|
||||
oldSize = page.viewportSize()
|
||||
# *****重点就是这里******
|
||||
page.setViewportSize(size)
|
||||
frame.render(painter)
|
||||
painter.end()
|
||||
image.save('Data/ScreenShotPage.png', 'png')
|
||||
|
||||
# 截图完成后需要还原,否则界面不响应鼠标等
|
||||
page.setViewportSize(oldSize)
|
||||
|
||||
os.startfile(os.path.abspath('Data/ScreenShotPage.png'))
|
||||
QMessageBox.information(self, '提示', '截图完成')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.excepthook = cgitb.enable(1, None, 5, '')
|
||||
app = QApplication(sys.argv)
|
||||
w = Window()
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
|
@ -128,6 +128,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
|
|||
- [梦幻树](QWebView/DreamTree.py)
|
||||
- [获取Cookie](QWebView/GetCookie.py)
|
||||
- [和Js交互操作](QWebView/JsSignals.py)
|
||||
- [网页整体截图](QWebView/ScreenShotPage.py)
|
||||
- [QWebEngineView](QWebEngineView)
|
||||
- [获取Cookie](QWebEngineView/GetCookie.py)
|
||||
- [和Js交互操作](QWebEngineView/JsSignals.py)
|
||||
|
|
Loading…
Reference in a new issue