[Python] 纯文本查看 复制代码 from PyPDF2 import PdfReader, PdfWriter
import os
def add_watermark_to_pdfs(folder_path, watermark_pdf, output_folder):
# 确保输出文件夹存在
os.makedirs(output_folder, exist_ok=True)
# 读取文件夹中的所有PDF文件
for filename in os.listdir(folder_path):
if filename.endswith('.pdf'):
pdf_reader = PdfReader(os.path.join(folder_path, filename))
pdf_writer = PdfWriter()
watermark_reader = PdfReader(watermark_pdf)
watermark_page = watermark_reader.pages[0]
for page in pdf_reader.pages:
page.merge_page(watermark_page)
pdf_writer.add_page(page)
# with open(output_pdf, 'wb') as out:
# pdf_writer.write(out)
# 保存添加水印后的PDF文件
with open(os.path.join(output_folder, filename), 'wb') as out:
pdf_writer.write(out)
# 调用函数批量添加水印
folder_path = 'pdf'
watermark_path = 'imgs/vft_logo.PDF'#水印文件存储为pdf
output_folder = 'watermark'
add_watermark_to_pdfs(folder_path, watermark_path, output_folder) |