逐页编辑pdf

时间:2019-05-05 04:10:16

标签: python pdf pypdf2

我正在尝试对预先存在的pdf中的各个页面进行唯一的编辑。但是,编辑保持不变。

我尝试使用FPDF(不确定如何用它来编辑先前存在的pdf),然后现在尝试使用reportlab的PYPDF2。

#
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

def WriteOnPdf (targetpdf, pageTopicsDict):
    packet = io.BytesIO()
    # Create a new PDF with Reportlab
    can = canvas.Canvas(packet, pagesize=letter)
    can.setFont('Helvetica', 13)
    can.drawString(5, 730, pageTopicsDict[0])
    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(targetpdf, "rb"))
    output = PdfFileWriter()
    # Add the "watermark" (which is the new pdf) on the existing page

    for i in range(existing_pdf.numPages):
        print(i, pageTopicsDict[i])
        can.drawString(5, 730, pageTopicsDict[i])
        page = existing_pdf.getPage(i)
        page.mergePage(new_pdf.getPage(0))# index out of range if not set to 0.
        output.addPage(page)    

    # Finally, write "output" to a real file
    outputStream = open("destination.pdf", "wb")
    output.write(outputStream)
    outputStream.close()

dummyDict = {0: "abc", 1: "de, fg", 2: "hijklmn"}
WriteOnPdf ("test.pdf", dummyDict)

预期:pdf,第0页的左上角带有“ abc”,第1页的“ de,fg”,第2页的“ hijklmn” ... 实际:所有页面都有“ abc”

1 个答案:

答案 0 :(得分:0)

已解决;在for循环中而不是在外部初始化数据包和相关变量。