diff --git a/pdf_unlock/install.reg b/pdf_unlock/install.reg new file mode 100644 index 0000000..be4408c --- /dev/null +++ b/pdf_unlock/install.reg @@ -0,0 +1,22 @@ +Windows Registry Editor Version 5.00 + +[HKEY_CLASSES_ROOT\SystemFileAssociations\.pdf\shell\RemovePermissions] +@="移除权限" +"MultiSelectModel"="Player" + +[HKEY_CLASSES_ROOT\SystemFileAssociations\.pdf\shell\RemovePermissions\command] +@="\"C:\\Program Files\\pdfUnlock\\main.exe\" \"%1\"" + +REM 添加文件夹右键菜单 +[HKEY_CLASSES_ROOT\Directory\shell\RemovePermissions] +@="移除PDF权限" + +[HKEY_CLASSES_ROOT\Directory\shell\RemovePermissions\command] +@="\"C:\\Program Files\\pdfUnlock\\main.exe\" \"%1\"" + +REM 添加到文件夹背景右键菜单 +[HKEY_CLASSES_ROOT\Directory\Background\shell\RemovePermissions] +@="移除PDF权限" + +[HKEY_CLASSES_ROOT\Directory\Background\shell\RemovePermissions\command] +@="\"C:\\Program Files\\pdfUnlock\\main.exe\" \"%V\"" \ No newline at end of file diff --git a/pdf_unlock/main.py b/pdf_unlock/main.py new file mode 100644 index 0000000..14cdf4e --- /dev/null +++ b/pdf_unlock/main.py @@ -0,0 +1,117 @@ +import PyPDF2 # PyMuPDF +import sys +from pathlib import Path + +def copy_pdf_pages(input_path: str, output_path: str) -> bool: + """ + 绉婚櫎PDF鏂囦欢鐨勬墍鏈夐檺鍒 + + Args: + input_path: 杈撳叆PDF鏂囦欢璺緞 + output_path: 杈撳嚭PDF鏂囦欢璺緞 + password: PDF瀵嗙爜锛堝鏋滄湁锛 + + Returns: + 鏄惁鎴愬姛绉婚櫎闄愬埗 + """ + try: + with open(input_path, 'rb') as input_file: + reader = PyPDF2.PdfReader(input_file) + + writer = PyPDF2.PdfWriter() + + # 澶嶅埗鎵鏈夐〉闈 + for page in reader.pages: + writer.add_page(page) + + # 鍐欏叆鏂版枃浠讹紙涓嶈缃换浣曞姞瀵嗘垨闄愬埗锛 + with open(output_path, 'wb') as output_file: + writer.write(output_file) + + return True + + except Exception as e: + print(f"绉婚櫎PDF闄愬埗鏃跺彂鐢熼敊璇: {e}") + return False + +# def copy_pdf_pages(input_file, output_file): +# """ +# 璇诲彇PDF鏂囦欢骞堕愰〉澶嶅埗鍒版柊鐨凱DF鏂囦欢 + +# Args: +# input_file (str): 杈撳叆PDF鏂囦欢璺緞 +# output_file (str): 杈撳嚭PDF鏂囦欢璺緞 +# """ +# try: +# # 妫鏌ヨ緭鍏ユ枃浠舵槸鍚﹀瓨鍦 +# if not os.path.exists(input_file): +# print(f"閿欒锛氳緭鍏ユ枃浠 '{input_file}' 涓嶅瓨鍦") +# return False + +# # 鎵撳紑杈撳叆PDF鏂囦欢 +# pdf_document = fitz.open(input_file) + +# # 鍒涘缓鏂扮殑PDF鏂囨。 +# new_pdf = fitz.open() +# new_pdf.insert_pdf(pdf_document) + +# # 淇濆瓨杈撳嚭鏂囦欢 +# new_pdf.save(output_file) + +# # 鍏抽棴鏂囨。 +# pdf_document.close() +# new_pdf.close() + +# return True + +# except FileNotFoundError: +# print(f"閿欒锛氭壘涓嶅埌鏂囦欢 '{input_file}'") +# return False +# except PermissionError: +# print(f"閿欒锛氭潈闄愪笉瓒筹紝鏃犳硶璁块棶鏂囦欢") +# return False +# except Exception as pdf_error: +# error_msg = str(pdf_error).lower() +# if "damaged" in error_msg or "corrupt" in error_msg: +# print(f"閿欒锛歅DF鏂囦欢 '{input_file}' 宸叉崯鍧") +# else: +# print(f"鍙戠敓閿欒锛歿str(pdf_error)}") +# return False + +def main(): + """涓诲嚱鏁""" + if len(sys.argv) < 2: + print("鐢ㄦ硶锛歱ython main.py <杈撳叆PDF鏂囦欢鎴栫洰褰>") + sys.exit(1) + + if len(sys.argv) > 2: + files = list(map(Path, sys.argv[1:])) + else: + input_path = Path(sys.argv[1]) + if input_path.is_dir(): + files = list(input_path.glob("**/*.pdf")) + else: + print("姝e湪澶勭悊",input_path.name) + output_file = input_path.with_name(f"{input_path.stem}_decrypt.pdf") + success = copy_pdf_pages(input_path, output_file) + print("澶勭悊瀹屾垚" if success else "澶勭悊澶辫触") + return + + total = len(files) + # 鎵ц澶嶅埗鎿嶄綔 + for i, pdf_file in enumerate(files, start=1): + rate= round(i/total *100) + print(f"杩涘害: ", "-"* (rate//5)," "*(20-rate//5), f" {rate}%",sep="",end="\r") + import time + # time.sleep(1) # 妯℃嫙澶勭悊鏃堕棿 + if not pdf_file.is_file(): + print(f"璺宠繃闈濸DF鏂囦欢锛歿pdf_file}") + continue + output_file = pdf_file.with_name(f"{pdf_file.stem}_decrypt.pdf") + success = copy_pdf_pages(pdf_file, output_file) + + if not success: + print(f"{pdf_file.name} 澶勭悊澶辫触") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/pdf_unlock/requirements.txt b/pdf_unlock/requirements.txt new file mode 100644 index 0000000..d2e6a9e --- /dev/null +++ b/pdf_unlock/requirements.txt @@ -0,0 +1 @@ +PyMuPDF==1.23.26