# -*- coding: utf-8 -*- from PyPDF2 import PdfFileReader, PdfFileWriter # PDF文档加密 def demo1(): pdf_reader = PdfFileReader('file/7_4.pdf') pdf_writer = PdfFileWriter() for page in range(pdf_reader.getNumPages()): pdf_writer.addPage(pdf_reader.getPage(page)) pdf_writer.encrypt('123456') # 设置文件的密码为 123456 with open('file/new_file/7_4_加密后.pdf', 'wb') as p: pdf_writer.write(p) p.close() # PDF文档解密 def demo2(): pdf_reader = PdfFileReader('file/new_file/7_4_加密后.pdf') # 输入正确的文档密码 pdf_reader.decrypt('123456') print(pdf_reader.getNumPages()) print(pdf_reader.xref) # 调用函数 demo1() demo2()