发布于 5年前

Python 3.x 使用PyPDF2在已有的PDF文件添加文本文字

有时我们需要向指定的pdf添加一些文字,在python 3.x 提供了PyPDF2和io.BytesIO,我们可以使用它们来完成次任务。

Python3.x 示例如下:

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = io.BytesIO()
# 使用Reportlab创建一个新的PDF
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(10, 100, "Hello world")
can.save()
#buffer从偏移0开始
packet.seek(0)
new_pdf = PdfFileReader(packet)
#读取已有的PDF
existing_pdf = PdfFileReader(open("original.pdf", "rb"))
output = PdfFileWriter()
# 
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# 最后,向目标的pdf写出
outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()

如果是Python2.7,可以使用pyPdf和StringIO来向指定的pdf添加一些文字。

Python2.7示例如下:

from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
packet = StringIO.StringIO()
# 使用Reportlab创建新的PDF
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(10, 100, "Hello world")
can.save()
#StringIO buffer从偏移0开始
packet.seek(0)
new_pdf = PdfFileReader(packet)
# 读取已有的PDF
existing_pdf = PdfFileReader(file("original.pdf", "rb"))
output = PdfFileWriter()
# 
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# 最后,向目的pdf写出
outputStream = file("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
©2020 edoou.com   京ICP备16001874号-3