from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = StringIO.StringIO()
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)
# create a new PDF with Reportlab
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()
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)
# create a new PDF with Reportlab
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.pages[0]
page.merge_page(new_pdf.pages[0])
output.add_page(page)
# finally, write "output" to a real file
output_stream = open("destination.pdf", "wb")
output.write(output_stream)
output_stream.close()
io.BytesIO
作为包,并且使用PyPDF2而不是未维护的pyPDF。很棒的答案! - Noufal Ibrahimopen
而不是file
会更好。 - mitenkaexisting_pdf
复制其余页面到output
很容易,示例代码只是没有这样做。 - alexis我知道这是一篇旧帖子,但我花了很长时间寻找解决方案。我发现只使用ReportLab和PyPDF就可以解决问题,因此我想分享一下我的方法:
PdfFileReader()
读取您的PDF文件,我们将其称为inputPdfFileReader()
读取该字符串对象,我们将其称为textPdfFileWriter()
创建一个新的PDF对象,我们将其称为output.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)
从这里,您可以将输入文件的页面与另一个文档合并。
pdfrw可以让您从现有的PDF中读取页面并将其绘制到ReportLab画布上(类似于绘制图像)。在GitHub上的pdfrwexamples/rl1子目录中有相关示例。免责声明:我是pdfrw的作者。
cpdf可以通过命令行完成工作。虽然不是Python(据我所知),但它可以帮助您。
cpdf -add-text "Line of text" input.pdf -o output .pdf
借鉴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()
截至撰写日期,PyPDF2已经停用了PdfFileReader、PdfFileWriter和其他一些方法,并将其更改为不同的名称和方法。还直接将getPage()方法更改为PdfReader的属性。
以下是一个非常简单的类来向现有的pdf文件添加文本: (使用示例在末尾演示)
from PyPDF2 import PdfWriter, PdfReader, Transformation
import io
from reportlab.pdfgen.canvas import Canvas
class GenerateFromTemplate:
def __init__(self,template):
self.template_pdf = PdfReader(open(template, "rb"))
self.template_page= self.template_pdf.pages[0]
self.packet = io.BytesIO()
self.c = Canvas(self.packet,pagesize=(self.template_page.mediabox.width,self.template_page.mediabox.height))
def addText(self,text,point):
self.c.drawString(point[0],point[1],text)
def merge(self):
self.c.save()
self.packet.seek(0)
result_pdf = PdfReader(self.packet)
result = result_pdf.pages[0]
self.output = PdfWriter()
op = Transformation().rotate(0).translate(tx=0, ty=0)
result.add_transformation(op)
self.template_page.merge_page(result)
self.output.add_page(self.template_page)
def generate(self,dest):
outputStream = open(dest,"wb")
self.output.write(outputStream)
outputStream.close()
"""
Use as:
gen = GenerateFromTemplate("template.pdf")
gen.addText("Hello!",(100,200))
gen.addText("PDF!",(100,300))
gen.merge()
gen.generate("Output.pdf")
"""
不要使用mergePage,因为它可能无法处理某些PDF文件。你应该使用mergeRotatedTranslatedPage。
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen.canvas import Canvas
page_to_merge = 0 #Refers to the First page of PDF
xcoor = 250 #To be changed according to your pdf
ycoor = 650 #To be changed according to your pdf
input_pdf = PdfFileReader(open("Source.pdf", "rb"))
page_count = input_pdf.getNumPages()
inputpdf_page_to_be_merged = input_pdf.getPage(page_to_merge)
packet = io.BytesIO()
c = Canvas(packet,pagesize=(inputpdf_page_to_be_merged.mediaBox.getWidth(),inputpdf_page_to_be_merged.mediaBox.getHeight()))
c.drawString(xcoor,ycoor,"Hello World")
c.save()
packet.seek(0)
overlay_pdf = PdfFileReader(packet)
overlay = overlay_pdf.getPage(0)
output = PdfFileWriter()
for PAGE in range(page_count):
if PAGE == page_to_merge:
inputpdf_page_to_be_merged.mergeRotatedTranslatedPage(overlay,
inputpdf_page_to_be_merged.get('/Rotate') or 0,
overlay.mediaBox.getWidth()/2, overlay.mediaBox.getWidth()/2)
output.addPage(inputpdf_page_to_be_merged)
else:
Page_in_pdf = input_pdf.getPage(PAGE)
output.addPage(Page_in_pdf)
outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
你可以尝试将问题分解成将PDF转换为可编辑格式,编写更改,然后再将其转换回PDF。我不知道是否有直接编辑PDF的库,但是例如DOC和PDF之间有很多转换器。