如何使用 Python 将网页转换为 PDF

新手上路,请多包涵

我正在寻找使用 Python 将网页打印成本地文件 PDF 的解决方案。一个好的解决方案是使用 Qt,可在此处找到, https://bharatikunal.wordpress.com/2010/01/

它在开始时没有工作,因为我在安装 PyQt4 时遇到问题,因为它给出了诸如“ ImportError: No module named PyQt4.QtCore ”和“ ImportError: No module named PyQt4.QtCore ”之类的错误消息。

这是因为 PyQt4 没有正确安装。我以前的库位于 C:\Python27\Lib,但它不适用于 PyQt4。

事实上,它只需要从 http://www.riverbankcomputing.com/software/pyqt/download 下载(注意您使用的是正确的 Python 版本),然后将其安装到 C:\Python27(我的例子)。而已。

现在脚本运行良好,所以我想分享它。有关使用 Qprinter 的更多选项,请参阅 http://qt-project.org/doc/qt-4.8/qprinter.html#Orientation-enum

原文由 Mark K 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 450
2 个回答

感谢以下帖子,我能够在生成的 PDF 上添加要打印的网页链接地址和当前时间,无论它有多少页。

使用 Python 将文本添加到现有 PDF

https://github.com/disflux/django-mtr/blob/master/pdfgen/doc_overlay.py

共享脚本如下:

 import time
from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from xhtml2pdf import pisa
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

url = 'http://www.yahoo.com'
tem_pdf = "c:\\tem_pdf.pdf"
final_file = "c:\\younameit.pdf"

app = QApplication(sys.argv)
web = QWebView()
#Read the URL given
web.load(QUrl(url))
printer = QPrinter()
#setting format
printer.setPageSize(QPrinter.A4)
printer.setOrientation(QPrinter.Landscape)
printer.setOutputFormat(QPrinter.PdfFormat)
#export file as c:\tem_pdf.pdf
printer.setOutputFileName(tem_pdf)

def convertIt():
    web.print_(printer)
    QApplication.exit()

QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)

app.exec_()
sys.exit

# Below is to add on the weblink as text and present date&time on PDF generated

outputPDF = PdfFileWriter()
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.setFont("Helvetica", 9)
# Writting the new line
oknow = time.strftime("%a, %d %b %Y %H:%M")
can.drawString(5, 2, url)
can.drawString(605, 2, oknow)
can.save()

#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(file(tem_pdf, "rb"))
pages = existing_pdf.getNumPages()
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
for x in range(0,pages):
    page = existing_pdf.getPage(x)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)
# finally, write "output" to a real file
outputStream = file(final_file, "wb")
output.write(outputStream)
outputStream.close()

print final_file, 'is ready.'

原文由 Mark K 发布,翻译遵循 CC BY-SA 3.0 许可协议

您还可以使用 pdfkit

用法

import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')

安装

MacOS: brew install Caskroom/cask/wkhtmltopdf

Debian/Ubuntu: apt-get install wkhtmltopdf

视窗: choco install wkhtmltopdf

请参阅 MacOS/Ubuntu/其他操作系统的官方文档: https ://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

原文由 NorthCat 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题