Python PDF添加水印

2021年6月29日 6点热度 0条评论 来源: lxccc9

PDF添加水印

加载模块:

from PyPDF2 import PdfFileReader,PdfFileWriter
import os

PDF添加水印:

watermark_pdf = PdfFileReader('./tests/watermark.pdf')
# 读取第一页
watermark = watermark_pdf.getPage(0)
# 读取需要加水印的pdf文件
input_pdf = PdfFileReader('./tests/daily_report.pdf')
# 创建写入文件
writer = PdfFileWriter()

for i in range(input_pdf.getNumPages()):
    # 获取每一页的对象:
    pag = input_pdf.getPage(i)
    # 合并页:
    pag.mergePage(watermark)
    # 将合并后的页加入writer
    writer.addPage(pag)

# 创建带水印pdf文件
with open('./tests/watermark_pdf.pdf','wb') as f:
    writer.write(f)

批量添加水印:

watermark_pdf = PdfFileReader('./tests/watermark.pdf')
# 读取第一页
watermark = watermark_pdf.getPage(0)
# 读取需要加水印的pdf文件
input_pdf = PdfFileReader('./tests/daily_report.pdf')
# 创建写入文件
writer = PdfFileWriter()

for i in range(input_pdf.getNumPages()):
    # 获取每一页的对象:
    pag = input_pdf.getPage(i)
    # 合并页:
    pag.mergePage(watermark)
    # 将合并后的页加入writer
    writer.addPage(pag)

# 创建带水印pdf文件
# with open('./tests/watermark_pdf.pdf','wb') as f:
# writer.write(f)

# 批量加水印

file_path = "C:/Users/godfather/pythonProject/hom/abc"
# print(os.listdir(file_path))
list_file = os.listdir(file_path)

for i in list_file:
    join_path = os.path.join(file_path,i)
    with open(join_path, 'wb') as f:
        writer.write(f)
    原文作者:lxccc9
    原文地址: https://blog.csdn.net/lxccc9/article/details/118346515
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。