我有一个类似于this的文件。
我想做的是用我自己的页脚替换页面的页脚。最好的方法是什么?我可以裁剪页脚部分(从底部开始固定大小),然后将自己创建的页脚合并到每个页面中吗?还是有可用的库可以自动从页面中提取页脚?我在这方面没有太多经验。我尝试了一些库,包括pypdf2和reportlab,但找不到有关页脚提取的任何帮助。
任何帮助将不胜感激。
这是一个hack解决方案,使用所需的页脚文本创建图像,然后运行它。根据需要调整坐标。
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.drawImage('yourFooterImage.png', 0, 2, 800, 45)
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()
我的输出: