Heim >Backend-Entwicklung >Python-Tutorial >Python中使用PyQt把网页转换成PDF操作代码实例

Python中使用PyQt把网页转换成PDF操作代码实例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-10 15:14:381801Durchsuche

代码很简单,功能也很简单 =w=

webpage2pdf

#!/usr/bin/env python3
 
import sys
 
try:
 from PyQt4 import QtWebKit
 from PyQt4.QtCore import QUrl
 from PyQt4.QtGui import QApplication, QPrinter
except ImportError:
 from PySide import QtWebKit
 from PySide.QtCore import QUrl
 from PySide.QtGui import QApplication, QPrinter
 
app = QApplication(sys.argv)
 
def done(status):
 p = QPrinter()
 p.setOutputFormat(QPrinter.PdfFormat)
 p.setOutputFileName('a.pdf')
 view.print(p)
 app.exit()
 
view = QtWebKit.QWebView()
view.load(QUrl('http://lilydjwg.is-programmer.com/'))
view.loadFinished[bool].connect(done)
# PySide does not have QApplication.exec
app.exec_()

注意:虽然没有图形界面,但是还是需要 X 连接……

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn