[color=]PDF
Password Remover 写着批量删除,结果要一个文件一个文件的输入密码,哪怕相同的密码也要一个一个CV.
于是 自己弄了一个.要win10 3.12写的.一般用不上 ,用到感觉挺实用.
微信截图_20250114093239.png (17.28 KB, 下载次数: 0)
下载附件
2025-1-14 09:32 上传
根据下面大佬给出的意见,做了修改,在pdf打开密码未填写时,直接读取脚本目录下的pass.xlsx中的第1列 文件名 和第2列 密码
微信截图_20250114140256.png (17.6 KB, 下载次数: 0)
下载附件
2025-1-14 14:03 上传
[Python] 纯文本查看 复制代码import os
import PyPDF2
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
import sys
def getpass(file_name, excel_file='pass.xlsx'):
try:
df = pd.read_excel(excel_file, header=None)
passrow = df[df.iloc[:, 0] == file_name]
if not passrow.empty:
return str(passrow.iloc[0, 1])
else:
return None
except Exception as e:
print(f"读取Excel错误: {e}")
return None
def kpdf(input_pdf, output_pdf, password):
try:
with open(input_pdf, 'rb') as input_file:
reader = PyPDF2.PdfReader(input_file)
if reader.is_encrypted:
if not reader.decrypt(password):
print(f"密码错误: {input_pdf}")
return False
writer = PyPDF2.PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open(output_pdf, 'wb') as output_file:
writer.write(output_file)
print(f"清除密码: {input_pdf}")
return True
except Exception as e:
return False
def clearpass(ifd, ofd, password=None):
if not os.path.exists(ofd):
os.makedirs(ofd)
for fname in os.listdir(ifd):
if fname.endswith(".pdf"):
input_pdf = os.path.join(ifd, fname)
output_pdf = os.path.join(ofd, fname)
f_pass = password if password else getpass(fname)
if f_pass and kpdf(input_pdf, output_pdf, f_pass):
print(f"转换成功: {fname}")
else:
print(f"转换失败: {fname}")
def ifd():
folder = filedialog.askdirectory()
ifd_var.set(folder)
def ofd():
folder = filedialog.askdirectory()
ofd_var.set(folder)
def doit():
ifd = os.path.normpath(ifd_var.get())
ofd = os.path.normpath(ofd_var.get())
password = pass_var.get()
if not ifd or not ofd:
messagebox.showerror("错误", "请填写 PDF 原始文件夹和输出文件夹.")
return
clearpass(ifd, ofd, password)
messagebox.showinfo("提示", "处理完成.")
root = tk.Tk()
root.title("PDF密码批量删除工具")
ifd_var = tk.StringVar()
ofd_var = tk.StringVar()
pass_var = tk.StringVar()
tk.Label(root, text="原始PDF文件夹:").grid(row=0, column=0, padx=10, pady=10)
tk.Entry(root, textvariable=ifd_var, width=50).grid(row=0, column=1, padx=10, pady=10)
tk.Button(root, text="浏览", command=ifd).grid(row=0, column=2, padx=10, pady=10)
tk.Label(root, text="输出PDF文件夹:").grid(row=1, column=0, padx=10, pady=10)
tk.Entry(root, textvariable=ofd_var, width=50).grid(row=1, column=1, padx=10, pady=10)
tk.Button(root, text="浏览", command=ofd).grid(row=1, column=2, padx=10, pady=10)
tk.Label(root, text="PDF打开密码:").grid(row=2, column=0, padx=10, pady=10)
tk.Entry(root, textvariable=pass_var, width=50, show="*").grid(row=2, column=1, padx=10, pady=10)
tk.Button(root, text="开始处理", command=doit).grid(row=3, column=1, padx=10, pady=20)
for i, var in enumerate([ifd_var, ofd_var, pass_var], start=1):
if i