如何在 python 的 PDF 文件的所有页面中添加水印?
How to add watermark in all pages of PDF files with python?
我正在尝试为我的 PDF 的每一页添加水印file.MyPDF 文件有 58 页,但我的输出文件只有最后一页在我的 PDF 文件中。
这是我的代码:
from PyPDF2 import PdfFileReader, PdfFileWriter
watermark_pdf = PdfFileReader("watermark.pdf")
watermark_page = watermark_pdf.getPage(0)
reader = PdfFileReader("original_document.pdf")
for page in reader.pages:
page.mergePage(watermark_page)
output = PdfFileWriter()
output.addPage(page)
with open("watermarked_document.pdf", "wb") as fp:
output.write(fp)
请告诉我如何给所有页面加水印。
您正在为每个页面重写 "merged" 文件。
试试
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)
相反。
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)
我的代码接收作为 cmd promt 参数传入的多个 pdf 文件,将它们合并到 super.pdf,然后在 super.pdf 中的每个页面添加水印,然后将其保存为 [=15] =]
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)
或者如果你有一个包含许多 pdf 的文件夹并且想为所有这些文件添加水印并将它们保存到新文件中,你可以使用它
*假设带有水印的文件名为wtr.pdf,第一页包含水印:
import PyPDF2
import pathlib
path = 'D:\Python\PDF processing' #add your folder path here
folder = pathlib.Path(path).resolve()
for item in folder.iterdir():
if item.is_file() and item.suffix == '.pdf' and not item.name == 'wtr.pdf':
with open(f'{item}', 'rb') as file, open('wtr.pdf', 'rb') as wtr:
input_file_reader = PyPDF2.PdfFileReader(file) # input file reader
wtr_reader = PyPDF2.PdfFileReader(wtr) # watermark reader
wtr_page = wtr_reader.getPage(0) # getting the page with the watermark
output = PyPDF2.PdfFileWriter() # the output writer
for i in range(input_file_reader.numPages): # looping the input reader pages
page = input_file_reader.getPage(i) # getting pages one by one
page.mergePage(wtr_page) # merging each page with the watermark
output.addPage(page) # adding the merged page to the output writer
with open(f'merged_{item.name}', 'wb') as merged_file:
output.write(merged_file) # saving to a file from the output writer
我正在尝试为我的 PDF 的每一页添加水印file.MyPDF 文件有 58 页,但我的输出文件只有最后一页在我的 PDF 文件中。
这是我的代码:
from PyPDF2 import PdfFileReader, PdfFileWriter
watermark_pdf = PdfFileReader("watermark.pdf")
watermark_page = watermark_pdf.getPage(0)
reader = PdfFileReader("original_document.pdf")
for page in reader.pages:
page.mergePage(watermark_page)
output = PdfFileWriter()
output.addPage(page)
with open("watermarked_document.pdf", "wb") as fp:
output.write(fp)
请告诉我如何给所有页面加水印。
您正在为每个页面重写 "merged" 文件。
试试
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)
相反。
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)
我的代码接收作为 cmd promt 参数传入的多个 pdf 文件,将它们合并到 super.pdf,然后在 super.pdf 中的每个页面添加水印,然后将其保存为 [=15] =]
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)
或者如果你有一个包含许多 pdf 的文件夹并且想为所有这些文件添加水印并将它们保存到新文件中,你可以使用它 *假设带有水印的文件名为wtr.pdf,第一页包含水印:
import PyPDF2
import pathlib
path = 'D:\Python\PDF processing' #add your folder path here
folder = pathlib.Path(path).resolve()
for item in folder.iterdir():
if item.is_file() and item.suffix == '.pdf' and not item.name == 'wtr.pdf':
with open(f'{item}', 'rb') as file, open('wtr.pdf', 'rb') as wtr:
input_file_reader = PyPDF2.PdfFileReader(file) # input file reader
wtr_reader = PyPDF2.PdfFileReader(wtr) # watermark reader
wtr_page = wtr_reader.getPage(0) # getting the page with the watermark
output = PyPDF2.PdfFileWriter() # the output writer
for i in range(input_file_reader.numPages): # looping the input reader pages
page = input_file_reader.getPage(i) # getting pages one by one
page.mergePage(wtr_page) # merging each page with the watermark
output.addPage(page) # adding the merged page to the output writer
with open(f'merged_{item.name}', 'wb') as merged_file:
output.write(merged_file) # saving to a file from the output writer