python中使用pyqt把网页转换成pdf操作代码实例

代码很简单,功能也很简单 =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 连接……

Posted in 未分类

发表评论