我需要使用Python为现有PDF添加一些额外的文本,最好的方法是什么以及我需要安装哪些额外的模块。
注意:理想情况下,我希望能够在Windows和Linux上运行此功能,但只需推送Linux即可。
答案 0 :(得分:96)
from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(10, 100, "Hello world")
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("original.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = file("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = io.BytesIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(10, 100, "Hello world")
can.save()
#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(open("original.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
答案 1 :(得分:75)
我知道这是一篇较老的帖子,但我花了很长时间试图找到解决方案。我只使用ReportLab和PyPDF遇到了一个不错的人,所以我想我会分享:
PdfFileReader()
阅读您的PDF,我们称之为输入 PdfFileReader()
读取字符串对象,我们将其称为 text PdfFileWriter()
创建一个新的PDF对象,我们称之为输出 .mergePage(*text*.getPage(0))
,然后使用output.addPage()
将修改后的页面添加到新文档中这适用于简单的文本添加。请参阅PyPDF的样本,为文档加水印。
以下是一些回答以下问题的代码:
packet = StringIO.StringIO()
can = canvas.Canvas(packet, pagesize=letter)
<do something with canvas>
can.save()
packet.seek(0)
input = PdfFileReader(packet)
从这里,您可以将输入文件的页面与另一个文档合并。
答案 2 :(得分:6)
pdfrw将允许您从现有PDF中读取页面并将其绘制到reportlab画布(类似于绘制图像)。在github上的pdfrw examples/rl1子目录中有这样的例子。免责声明:我是pdfrw作者。
答案 3 :(得分:6)
利用上面David Dehghan的answer,以下内容适用于Python 2.7.13:
from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = StringIO.StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(290, 720, "Hello world")
can.save()
#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader("original.pdf")
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
答案 4 :(得分:2)
cpdf将从命令行完成工作。它不是python,但是(afaik):
cpdf -add-text "Line of text" input.pdf -o output .pdf
答案 5 :(得分:0)
您可以更好地将问题分解为将PDF转换为可编辑格式,编写更改,然后将其转换回PDF。我不知道哪个库可以让你直接编辑PDF,但DOC和PDF之间有很多转换器。
答案 6 :(得分:0)
如果您使用的是Windows,则可能会有效:
还有一篇关于Python中PDF创建和编辑框架的白皮书。这有点过时了,但也许可以给你一些有用的信息:
答案 7 :(得分:-3)
您是否尝试过pyPdf?
抱歉,它无法修改网页内容。