python操作PDF------加水印及加密解密

# 首先制作水印文件  打开word制作一个只有水印的页面,保存为PDF

from PyPDF2 import PdfFileReader, PdfFileWriter
from copy import copy


# 合并PDF水印
watermark_pdf = PdfFileReader('水印.pdf')
watermark_page = watermark_pdf.getPage(0)
pdf_reader = PdfFileReader('基于python的网页爬虫.pdf')
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
    # 没有水印的页面
    original_page = pdf_reader.getPage(page)
    new_page = copy(watermark_page)
    # 加了水印的页面   下面的内容.mergePage(出现在上面的内容)
    new_page.mergePage(original_page)
    pdf_writer.addPage(new_page)
with open('watermarked.pdf', 'wb') as f:
    pdf_writer.write(f)


# 加密PDF
pdf_reader = PdfFileReader('基于python的网页爬虫.pdf')
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
    pdf_writer.addPage(pdf_reader.getPage(page))
# 输入密码
pdf_writer.encrypt('123456')
with open('encrypted.pdf', 'wb') as f:
    pdf_writer.write(f)


# 解密
pdf_reader = PdfFileReader('基于python的网页爬虫.pdf')
pdf_reader.decrypt('123456')
pdf_writer = PdfFileWriter()
for page in range(pdf_reader.getNumPages()):
    pdf_writer.addPage(pdf_reader.getPage(page))
# 输入密码
pdf_writer.encrypt('123456')
with open('dncrypted.pdf', 'wb') as f:
    pdf_writer.write(f)

 

posted @ 2020-09-04 19:16  不会飞的鲨鱼  阅读(1074)  评论(0编辑  收藏  举报