if not pdf_folder or not save_path or not password:
messagebox.showerror('错误', '请填写所有字段')
return
# 确保节存在,如果不存在则创建
if not config.has_section('Settings'):
config.add_section('Settings')
# 更新配置文件
config.set('Settings', 'LastPassword', password)
with open('config.ini', 'w') as config_file:
config.write(config_file)
# 遍历文件夹中的所有PDF文件
for filename in os.listdir(pdf_folder):
if filename.lower().endswith('.pdf'):
pdf_path = os.path.join(pdf_folder, filename)
with open(pdf_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
pdf_writer = PyPDF2.PdfWriter()
for page in pdf_reader.pages:
pdf_writer.add_page(page)
pdf_writer.encrypt(password)
new_file_path = os.path.join(save_path, filename)
with open(new_file_path, 'wb') as new_file:
pdf_writer.write(new_file)