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

Fro*_*kys 96 python pdf

我需要使用Python为现有PDF添加一些额外的文本,这是最好的方法,我需要安装哪些额外的模块.

注意:理想情况下,我希望能够在Windows和Linux上运行它,但只有推动Linux才能实现.

编辑:pyPDFReportLab看起来不错,但是没有人允许我编辑现有的PDF,还有其他选择吗?

Dav*_*han 107

[Python 2.7]的示例:

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()
Run Code Online (Sandbox Code Playgroud)

Python 3.x的示例:


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()
Run Code Online (Sandbox Code Playgroud)

  • 对于python3,数据包应为`io.BytesIO`并使用PyPDF2而不是pyPDF(未维护).很棒的答案! (11认同)
  • 感谢分享.它很棒.一个注意事项:我认为最好使用`open`而不是`file`. (3认同)
  • 注意:新文档仅包含原始文档的第一页!将其余页面从“existing_pdf”复制到“output”很容易,但示例代码却没有。 (3认同)

dwe*_*lch 81

我知道这是一篇较老的帖子,但我花了很长时间试图找到解决方案.我只使用ReportLab和PyPDF遇到了一个不错的人,所以我想我会分享:

  1. 使用读取您的PDF PdfFileReader(),我们将调用此输入
  2. 创建一个包含要使用ReportLab添加的文本的新pdf,将其另存为字符串对象
  3. 使用读取字符串对象PdfFileReader(),我们将调用此文
  4. 使用创建一个新的PDF对象PdfFileWriter(),我们将调用此输出
  5. 迭代输入并应用.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)
Run Code Online (Sandbox Code Playgroud)

从这里,您可以将输入文件的页面与另一个文档合并.

  • "创建一个包含要使用ReportLab添加的文本的新pdf,将其另存为字符串对象"你是如何做到的?它是一个画布实例. (2认同)
  • 这个答案是金色的-就像一个魅力。 (2认同)
  • 此代码将创建一个新的 pdf 文件并跳过所有元数据。所以它不会附加到现有的pdf。 (2认同)

Pat*_*pin 8

pdfrw将允许您从现有PDF中读取页面并将它们绘制到reportlab画布(类似于绘制图像).在github上的pdfrw examples/rl1子目录中有这样的例子.免责声明:我是pdfrw作者.


use*_*670 7

cpdf从命令行完成这项工作。不过,它不是python(afaik):

cpdf -add-text "Line of text" input.pdf -o output .pdf
Run Code Online (Sandbox Code Playgroud)

  • 使用前请仔细检查 cpdf 的许可证 - 它不是开源的。 (3认同)

Ros*_* II 7

利用David Dehghan的上述答案,以下内容适用于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()
Run Code Online (Sandbox Code Playgroud)