在将pdf与PyPDF2合并时添加页码

时间:2019-02-28 17:39:41

标签: python pdf pypdf2 pypdf

我正在将信息单位(.PDF)合并为单独的PDF。我有很多变量,其中有很多过滤器(您可以选择每个过滤器中的一个)来定义完整的手册,请看: Manual Generator in portuguese

我的主要问题基本上是在合并PDF时为其添加页码,我使用的是代码herehere,但未成功 我使用的简化代码是:

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

print(var1.get(), var2.get(), var3.get(), var4.get(), var5.get(), var6.get())
merger.append(Modelo) #filter one
merger.append(Escada) #filter two
output = open('ModeloMerg.pdf', 'wb')
merger.write(output)

pdf = PdfFileReader('ModeloMerg.pdf')

packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)
can.drawString(10, 100, "Page" + str(15)) #just a random test number
can.save()
packet.seek(0)

watermark = PdfFileReader(packet)
watermark_page = watermark.getPage(0)



for page in range(pdf.getNumPages()):

    pdf_page = pdf.getPage(page)
    pdf_page.mergePage(watermark_page)
    pdf_writer.addPage(pdf_page)

with open('out.pdf', 'wb') as fh:
    pdf_writer.write(fh)

1 个答案:

答案 0 :(得分:0)

我知道这是旧的,但我想出了一个解决方案,可能会帮助将来遇到这个问题的人。这不是最有效的,但这只是一个例子。

我使用 FPDF 和 PyPDF2 的组合来完成添加页码。如果您已经在合并文档,您可能可以修改我的简单示例以提高效率,但我这样做是因为我希望页码的文本为“[x] 中的 [x] 页”。

如果您不想列出总页码,可以简化它。

import os
import PyPDF2
from fpdf import FPDF


class NumberPDF(FPDF):
    def __init__(self, numberOfPages):
        super(NumberPDF, self).__init__()
        self.numberOfPages = numberOfPages

    # Overload Header
    def header(self):
        pass

    # Overload Footer
    def footer(self):
        self.set_y(-15)
        self.set_font('Arial', 'I', 8)
        self.cell(0, 10, f"Page {self.page_no()} of {self.numberOfPages}", 0, 0, 'C')


# Grab the file you want to add pages to
inputFile = PyPDF2.PdfFileReader("original.pdf")
outputFile = "originalWithPageNumbers.pdf"

# Create a temporary numbering PDF using the overloaded FPDF class, passing the number of pages
# from your original file
tempNumFile = NumberPDF(inputFile.getNumPages())

# Add a new page to the temporary numbering PDF (the footer function runs on add_page and will 
# put the page number at the bottom, all else will be blank
for page in range(inputFile.getNumPages()):
    tempNumFile.add_page()

# Save the temporary numbering PDF
tempNumFile.output("tempNumbering.pdf")

# Create a new PDFFileReader for the temporary numbering PDF
mergeFile = PyPDF2.PdfFileReader("tempNumbering.pdf")

# Create a new PDFFileWriter for the final output document
mergeWriter = PyPDF2.PdfFileWriter()

# Loop through the pages in the temporary numbering PDF
for x, page in enumerate(mergeFile.pages):
    # Grab the corresponding page from the inputFile
    inputPage = inputFile.getPage(x)
    # Merge the inputFile page and the temporary numbering page
    inputPage.mergePage(page)
    # Add the merged page to the final output writer
    mergeWriter.addPage(inputPage)

# Delete the temporary file
os.remove("tempNumbering.pdf")

# Write the merged output
with open(outputFile, 'wb') as fh:
    mergeWriter.write(fh)