Python如何将PDF转换为文本文件?使用PyPDF2在现有文件中添加文本内容
发布时间
阅读量:
阅读量
有时我们需要向特定的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
全部评论 (0)
还没有任何评论哟~
