去除PDF加密等权限限制

import PyPDF2
from Crypto.Cipher import AES
import os

def remove_pdf_restrictions():
origi_path = r'C:\Users\yc\PycharmProjects\netmiko\丹凤相关\original_file\\'
pdf_path = r'C:\Users\yc\PycharmProjects\netmiko\丹凤相关\pdf_result\\'
file_list = os.listdir(origi_path) # 查看当前路径的文件名
print(file_list)
file_list_result = [os.path.join(origi_path, i) for i in file_list]
print(file_list_result) # 显示当前稳健的绝对路径
for origi_file, result_file_name in zip (file_list_result,file_list):
pdf_file = open(origi_file, 'rb')
pdf_reader = PyPDF2.PdfReader(pdf_file)

if pdf_reader.is_encrypted:
pdf_reader.decrypt('')

pdf_writer = PyPDF2.PdfWriter()

for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
pdf_writer.add_page(page)

output_pdf_file = open(pdf_path + result_file_name , 'wb')
pdf_writer.write(output_pdf_file)

output_pdf_file.close()
pdf_file.close()

remove_pdf_restrictions()
posted @ 2023-12-08 13:25  小菜鸟起飞ing  阅读(45)  评论(0编辑  收藏  举报