# 可通过指定镜像源安装
# pip install PyPDF2 -i https://pypi.tuna.tsinghua.edu.cn/simple
import PyPDF2
def encrypt_pdf(input_file, output_file, password):
reader = PyPDF2.PdfReader(input_file)
writer = PyPDF2.PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open(output_file, 'wb') as f:
writer.encrypt(password)
writer.write(f)
# 要加密的文件名,替换为你的文件名,将文件放在与本脚本同级目录下
input_file = 'input.pdf'
# 加密后输出文件的文件名
output_file = 'output.pdf'
# 将下面置换为你的密码
password = '20200202'
if __name__ == "__main__":
encrypt_pdf(input_file, output_file, password)
03-16
2829
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-02
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交