Answers:
我知道这是一篇较旧的文章,但是我花了很长时间尝试寻找解决方案。我碰巧只使用ReportLab和PyPDF,所以我想分享一下:
PdfFileReader()
,我们称此输入PdfFileReader()
,我们将此文本称为PdfFileWriter()
,我们将其称为输出.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)
在这里,您可以将输入文件的页面与另一个文档合并。
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()
io.BytesIO
并使用PyPDF2而不是pyPDF(未维护)。好答案!
open
代替file
。
existing_pdf
到output
,这很容易,示例代码却没有。
pdfrw允许您读取现有PDF的页面并将其绘制到reportlab画布上(类似于绘制图像)。github上的pdfrw examples / rl1子目录中有一些示例。免责声明:我是pdfrw的作者。
利用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()
将问题分解为将PDF转换为可编辑格式,编写更改,然后再将其转换回PDF可能会更好。我不知道可以直接编辑PDF的库,但是例如在DOC和PDF之间有很多转换器。
如果您使用的是Windows,这可能会起作用:
还有一个Python中的PDF创建和编辑框架的白皮书。这有点过时,但也许可以给您一些有用的信息:
您尝试过pyPdf吗?
抱歉,它无法修改页面的内容。