python pdf删除图片_如何使用Python从PDF中删除文本

我正在创建一个python脚本来编辑PDF文本。

我有这个Python代码,它允许我将文本添加到PDF文件的特定位置。

import PyPDF2

import io

from reportlab.pdfgen import canvas

from reportlab.lib.pagesizes import letter

import sys

packet = io.BytesIO()

# create a new PDF with Reportlab

can = canvas.Canvas(packet, pagesize=letter)

# Insert code into specific position

can.drawString(300, 115, "Hello world")

can.save()

#move to the beginning of the StringIO buffer

packet.seek(0)

new_pdf = PyPDF2.PdfFileReader(packet)

# read your existing PDF

existing_pdf = PyPDF2.PdfFileReader(open("original.pdf", "rb"))

num_pages = existing_pdf.numPages

output = PyPDF2.PdfFileWriter()

# add the "watermark" (which is the new pdf) on the existing page

page = existing_pdf.getPage(num_pages-1) # get the last page of the original pdf

page.mergePage(new_pdf.getPage(0)) # merges my created text with my PDF.

x = existing_pdf.getNumPages()

#add all pages from original pdf into output pdf

for n in range(x):

output.addPage(existing_pdf.getPage(n))

# finally, write "output" to a real file

outputStream = open("output.pdf", "wb")

output.write(outputStream)

outputStream.close()

我的问题:我想用我的自定义文本替换原始PDF的特定位置的文本。一种写空白字符的方法可以解决问题,但我找不到任何可以做到这一点的事情。

PS。:它必须是Python代码,因为稍后我需要将其部署为.exe文件,我只知道如何使用Python代码执行此操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值