如何使用Python将网页转换为PDF
我正在寻找使用Python将网页打印为本地文件PDF的解决方案。很好的解决方案之一是使用Qt。
由于我在安装PyQt4时遇到问题,因此一开始并不起作用,因为它给出了错误消息,例如“ ImportError:没有名为PyQt4.QtCore的模块”和“ImportError:没有名为PyQt4.QtCore的模块”。
这是因为PyQt4没有正确安装。我曾经将库放在C:\ Python27 \ Lib中,但是它不适用于PyQt4。
实际上,它只需要从下载(注意要使用的正确Python版本),然后将其安装到C:\Python27(我的情况)。而已。
回答:
多亏了下面的帖子,无论页面有多少页,我都能添加要打印的网页链接地址并在生成的PDF上显示时间。
要共享脚本,如下所示:
import timefrom 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.'
以上是 如何使用Python将网页转换为PDF 的全部内容, 来源链接: utcz.com/qa/400359.html