本文整理汇总了Python中PyQt5.QtWebKitWidgets.QWebPage.viewportSize方法的典型用法代码示例。如果您正苦于以下问题:Python QWebPage.viewportSize方法的具体用法?Python QWebPage.viewportSize怎么用?Python QWebPage.viewportSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWebKitWidgets.QWebPage
的用法示例。
在下文中一共展示了QWebPage.viewportSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HTMLTableRenderer
# 需要导入模块: from PyQt5.QtWebKitWidgets import QWebPage [as 别名]
# 或者: from PyQt5.QtWebKitWidgets.QWebPage import viewportSize [as 别名]
class HTMLTableRenderer(QObject):
def __init__(self, html, base_dir, width, height, dpi, factor):
'''
`width, height`: page width and height in pixels
`base_dir`: The directory in which the HTML file that contains the table resides
'''
from calibre.gui2 import secure_web_page
QObject.__init__(self)
self.app = None
self.width, self.height, self.dpi = width, height, dpi
self.base_dir = base_dir
self.images = []
self.tdir = tempfile.mkdtemp(prefix='calibre_render_table')
self.loop = QEventLoop()
self.page = QWebPage()
secure_web_page(self.page.settings())
self.page.loadFinished.connect(self.render_html)
self.page.mainFrame().setTextSizeMultiplier(factor)
self.page.mainFrame().setHtml(html,
QUrl('file:'+os.path.abspath(self.base_dir)))
def render_html(self, ok):
try:
if not ok:
return
cwidth, cheight = self.page.mainFrame().contentsSize().width(), self.page.mainFrame().contentsSize().height()
self.page.setViewportSize(QSize(cwidth, cheight))
factor = float(self.width)/cwidth if cwidth > self.width else 1
cutoff_height = int(self.height/factor)-3
image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
image.setDotsPerMeterX(self.dpi*(100/2.54))
image.setDotsPerMeterY(self.dpi*(100/2.54))
painter = QPainter(image)
self.page.mainFrame().render(painter)
painter.end()
cheight = image.height()
cwidth = image.width()
pos = 0
while pos < cheight:
img = image.copy(0, pos, cwidth, min(cheight-pos, cutoff_height))
pos += cutoff_height-20
if cwidth > self.width:
img = img.scaledToWidth(self.width, Qt.SmoothTransform)
f = os.path.join(self.tdir, '%d.png'%pos)
img.save(f)
self.images.append((f, img.width(), img.height()))
finally:
QApplication.quit()
示例2: PageThumbnailer
# 需要导入模块: from PyQt5.QtWebKitWidgets import QWebPage [as 别名]
# 或者: from PyQt5.QtWebKitWidgets.QWebPage import viewportSize [as 别名]
#.........这里部分代码省略.........
self.__loadTitle = False
self.__title = ""
self.__url = QUrl()
self.__proxy = NetworkAccessManagerProxy(self)
import Helpviewer.HelpWindow
self.__proxy.setPrimaryNetworkAccessManager(
Helpviewer.HelpWindow.HelpWindow.networkAccessManager())
self.__page.setNetworkAccessManager(self.__proxy)
self.__page.mainFrame().setScrollBarPolicy(
Qt.Horizontal, Qt.ScrollBarAlwaysOff)
self.__page.mainFrame().setScrollBarPolicy(
Qt.Vertical, Qt.ScrollBarAlwaysOff)
# Full HD
# Every page should fit in this resolution
self.__page.setViewportSize(QSize(1920, 1080))
def setSize(self, size):
"""
Public method to set the size of the image.
@param size size of the image (QSize)
"""
if size.isValid():
self.__size = QSize(size)
def setUrl(self, url):
"""
Public method to set the URL of the site to be thumbnailed.
@param url URL of the web site (QUrl)
"""
if url.isValid():
self.__url = QUrl(url)
def url(self):
"""
Public method to get the URL of the thumbnail.
@return URL of the thumbnail (QUrl)
"""
return QUrl(self.__url)
def loadTitle(self):
"""
Public method to check, if the title is loaded from the web site.
@return flag indicating, that the title is loaded (boolean)
"""
return self.__loadTitle
def setLoadTitle(self, load):
"""
Public method to set a flag indicating to load the title from
the web site.
@param load flag indicating to load the title (boolean)
"""
self.__loadTitle = load
def title(self):
"""
Public method to get the title of the thumbnail.
@return title of the thumbnail (string)
"""
return self.__title
def start(self):
"""
Public method to start the thumbnailing action.
"""
self.__page.loadFinished.connect(self.__createThumbnail)
self.__page.mainFrame().load(self.__url)
def __createThumbnail(self, status):
"""
Private slot creating the thumbnail of the web site.
@param status flag indicating a successful load of the web site
(boolean)
"""
if not status:
self.thumbnailCreated.emit(QPixmap())
return
self.__title = self.__page.mainFrame().title()
image = QImage(self.__page.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
self.__page.mainFrame().render(painter)
painter.end()
scaledImage = image.scaled(self.__size,
Qt.KeepAspectRatioByExpanding,
Qt.SmoothTransformation)
self.thumbnailCreated.emit(QPixmap.fromImage(scaledImage))