如何使用python在PDF文件的所有页面中添加水印?

时间:2020-06-08 11:46:35

标签: python

我尝试在PDF文件的每一页中添加水印。我的PDF文件有58页,但是我的输出文件的PDF文件中只有最后一页。

My PDF file have 59 pages.

This's my PDF Output files.

这是我的代码:

import PyPDF2
from PyPDF2 import PdfFileMerger
from PyPDF2 import PdfFileReader, PdfFileWriter
pdf_file = "C:/Users/11359023/Desktop/deepfake_vee.pdf"
watermark = "C:/Users/11359023/Desktop/simple.pdf"
merged_file = "C:/Users/11359023/Desktop/merged.pdf"
input_file = open(pdf_file,'rb')
input_pdf = PyPDF2.PdfFileReader(input_file)
watermark_file = open(watermark,'rb')
watermark_pdf = PyPDF2.PdfFileReader(watermark_file)
ipdf = PdfFileReader(open('C:/Users/11359023/Desktop/deepfake_vee.pdf', 'rb'))
for i in range(ipdf.getNumPages()):
    pdf_page = input_pdf.getPage(i)
    watermark_page = watermark_pdf.getPage(0)
    pdf_page.mergePage(watermark_page)
    output = PyPDF2.PdfFileWriter()
    output.addPage(pdf_page)
    merged_file = open("C:/Users/11359023/Desktop/merged.pdf",'wb')
    output.write(merged_file)
merged_file.close()
watermark_file.close()
input_file.close()

请告诉我如何在所有页面上添加水印。

3 个答案:

答案 0 :(得分:1)

您正在为每个页面重写“合并”文件。

尝试类似

from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter

pdf_file = "C:/Users/11359023/Desktop/deepfake_vee.pdf"
watermark = "C:/Users/11359023/Desktop/simple.pdf"
merged = "C:/Users/11359023/Desktop/merged.pdf"

with open(pdf_file, "rb") as input_file, open(watermark, "rb") as watermark_file:
    input_pdf = PdfFileReader(input_file)
    watermark_pdf = PdfFileReader(watermark_file)
    watermark_page = watermark_pdf.getPage(0)

    output = PdfFileWriter()

    for i in range(input_pdf.getNumPages()):
        pdf_page = input_pdf.getPage(i)
        pdf_page.mergePage(watermark_page)
        output.addPage(pdf_page)

    with open(merged, "wb") as merged_file:
        output.write(merged_file)

相反。

答案 1 :(得分:0)

import PyPDF2

template = PyPDF2.PdfFileReader(open('C:/Users/11359023/Desktop/deepfake_vee.pdf', 'rb'))
watermark = PyPDF2.PdfFileReader(open('C:/Users/11359023/Desktop/simple.pdf', 'rb'))
output = PyPDF2.PdfFileWriter()

for i in range(template.getNumPages()):
  page = template.getPage(i)
  page.mergePage(watermark.getPage(0))
  output.addPage(page)

  with open('C:/Users/11359023/Desktop/merged.pdf', 'wb') as file:
    output.write(file)

答案 2 :(得分:0)

我的代码正在接收作为 cmd promt 参数传入的多个 pdf 文件,将它们合并到 super.pdf 中,然后在将其保存为 watermarked.pdf 后将水印添加到 super.pdf 中的每个页面

import PyPDF2
import sys

pdf_file_list = sys.argv[1:]
watermark = 'wtr.pdf'
merger = PyPDF2.PdfFileMerger()
writer = PyPDF2.PdfFileWriter()


for file in pdf_file_list:
    merger.append(file)

merger.write('super.pdf')


with open('super.pdf', 'rb') as fileinput:
    pdf_file = PyPDF2.PdfFileReader(fileinput)

    with open(watermark, 'rb') as filewatermark:
        watermark_pdf = PyPDF2.PdfFileReader(filewatermark)

        for page in range(pdf_file.getNumPages()):
            current_pdf_page = pdf_file.getPage(page)
            first_page_watermark = watermark_pdf.getPage(0)
            current_pdf_page.mergePage(first_page_watermark)
            writer.addPage(current_pdf_page)

            with open("watermarked.pdf", 'wb')as file_output:
                writer.write(file_output)