{"id":5509,"date":"2025-02-20T08:12:17","date_gmt":"2025-02-20T00:12:17","guid":{"rendered":"https:\/\/benson80.eu.org\/?p=5509"},"modified":"2025-02-20T08:48:54","modified_gmt":"2025-02-20T00:48:54","slug":"%e8%bd%bb%e6%9d%be%e6%89%b9%e9%87%8f%e8%a7%a3%e5%af%86%ef%bc%9a%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%94%81%e5%8a%a0%e5%af%86-pdf-%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/benson80.eu.org\/?p=5509","title":{"rendered":"\u8f7b\u677e\u6279\u91cf\u89e3\u5bc6\uff1a\u4f7f\u7528 Python \u89e3\u9501\u52a0\u5bc6 PDF \u6587\u4ef6"},"content":{"rendered":"\n<p>\u5728\u8fd9\u7bc7\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Python \u7f16\u5199\u4e00\u4e2a\u6279\u91cf\u89e3\u5bc6 PDF \u6587\u4ef6\u7684\u5de5\u5177\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u754c\u9762\uff08GUI\uff09\uff0c\u4ee5\u4fbf\u66f4\u8f7b\u677e\u5730\u64cd\u4f5c\u3002\u6211\u4eec\u5c06\u4f7f\u7528 <code>PyPDF2<\/code> \u5e93\u6765\u5904\u7406 PDF \u6587\u4ef6\uff0c\u5e76\u4f7f\u7528 <code>Tkinter<\/code> \u5e93\u521b\u5efa\u56fe\u5f62\u754c\u9762\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06\u8be5\u811a\u672c\u8f6c\u6362\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08<code>.exe<\/code>\uff09\uff0c\u5e76\u4e3a\u5176\u6dfb\u52a0\u81ea\u5b9a\u4e49\u56fe\u6807\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e24\u4e2a Python \u5e93\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>PyPDF2<\/strong>\uff1a\u7528\u4e8e\u8bfb\u53d6\u548c\u64cd\u4f5c PDF \u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>Tkinter<\/strong>\uff1a\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u754c\u9762\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5 <code>PyPDF2<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>pip install pypdf2<\/code><\/pre>\n\n\n\n<p><code>Tkinter<\/code> \u662f Python \u7684\u6807\u51c6\u5e93\uff0c\u901a\u5e38\u5df2\u7ecf\u5305\u542b\u5728\u5b89\u88c5\u5305\u4e2d\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u7f16\u5199\u6279\u91cf\u89e3\u5bc6 PDF \u6587\u4ef6\u7684 Python \u811a\u672c<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Python \u811a\u672c\uff0c\u80fd\u591f\u6279\u91cf\u89e3\u5bc6 PDF \u6587\u4ef6\u3002\u5b83\u5305\u62ec\u4e00\u4e2a\u56fe\u5f62\u754c\u9762\uff0c\u5141\u8bb8\u7528\u6237\u9009\u62e9\u8981\u89e3\u5bc6\u7684 PDF \u6587\u4ef6\u5e76\u8fdb\u884c\u6279\u91cf\u89e3\u5bc6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Python \u811a\u672c\uff1a<code>batch_pdf_decrypt_gui.py<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import os<br>from PyPDF2 import PdfReader, PdfWriter<br>import tkinter as tk<br>from tkinter import filedialog<br>from tkinter import messagebox<br><br>def decrypt_pdf(input_pdf, output_pdf):<br>    try:<br>        # \u6253\u5f00PDF\u6587\u4ef6<br>        with open(input_pdf, \"rb\") as infile:<br>            reader = PdfReader(infile)<br>            <br>            # \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u52a0\u5bc6<br>            if reader.is_encrypted:<br>                reader.decrypt(\"\")  # \u5982\u679c\u6ca1\u6709\u5bc6\u7801\uff0c\u5219\u4f20\u9012\u7a7a\u5b57\u7b26\u4e32\u5c1d\u8bd5\u53bb\u9664\u52a0\u5bc6<br>                writer = PdfWriter()<br>                <br>                # \u5c06\u6240\u6709\u9875\u9762\u5199\u5165\u65b0\u7684PDF\u6587\u4ef6<br>                for page_num in range(len(reader.pages)):<br>                    writer.add_page(reader.pages[page_num])<br><br>                # \u4fdd\u5b58\u89e3\u5bc6\u540e\u7684PDF\u6587\u4ef6<br>                with open(output_pdf, \"wb\") as outfile:<br>                    writer.write(outfile)<br>                print(f\"\u6210\u529f\u89e3\u5bc6\u5e76\u4fdd\u5b58\u6587\u4ef6: {output_pdf}\")<br>            else:<br>                print(f\"\u6587\u4ef6 {input_pdf} \u6ca1\u6709\u52a0\u5bc6\")<br>    except Exception as e:<br>        print(f\"\u89e3\u5bc6\u6587\u4ef6 {input_pdf} \u65f6\u51fa\u9519: {e}\")<br><br>def batch_decrypt_pdfs(pdf_files):<br>    for input_pdf in pdf_files:<br>        output_pdf = os.path.join(os.path.dirname(input_pdf), f\"decrypted_{os.path.basename(input_pdf)}\")<br>        decrypt_pdf(input_pdf, output_pdf)<br><br>def select_files():<br>    # \u8ba9\u7528\u6237\u9009\u62e9\u4e00\u4e2a\u6216\u591a\u4e2a PDF \u6587\u4ef6<br>    file_paths = filedialog.askopenfilenames(title=\"\u9009\u62e9\u9700\u8981\u89e3\u5bc6\u7684PDF\u6587\u4ef6\", filetypes=[(\"PDF files\", \"*.pdf\")])<br>    return file_paths<br><br>def on_decrypt_button_click():<br>    # \u83b7\u53d6\u9009\u62e9\u7684 PDF \u6587\u4ef6<br>    pdf_files = select_files()<br>    if not pdf_files:<br>        messagebox.showerror(\"\u9519\u8bef\", \"\u8bf7\u81f3\u5c11\u9009\u62e9\u4e00\u4e2a PDF \u6587\u4ef6\uff01\")<br>        return<br><br>    # \u5f00\u59cb\u89e3\u5bc6<br>    try:<br>        batch_decrypt_pdfs(pdf_files)<br>        messagebox.showinfo(\"\u6210\u529f\", \"PDF \u6587\u4ef6\u89e3\u5bc6\u6210\u529f\uff01\")<br>    except Exception as e:<br>        messagebox.showerror(\"\u9519\u8bef\", f\"\u89e3\u5bc6\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef: {e}\")<br><br># \u521b\u5efa\u4e3b\u7a97\u53e3<br>root = tk.Tk()<br>root.title(\"PDF \u6279\u91cf\u89e3\u5bc6\u5de5\u5177\")<br>root.geometry(\"400x200\")<br><br># \u89e3\u5bc6\u6309\u94ae<br>decrypt_button = tk.Button(root, text=\"\u89e3\u5bc6\u9009\u4e2d\u7684PDF\u6587\u4ef6\", command=on_decrypt_button_click)<br>decrypt_button.pack(pady=50)<br><br># \u8fd0\u884c\u4e3b\u5faa\u73af<br>root.mainloop()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u811a\u672c\u8bf4\u660e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>decrypt_pdf<\/code><\/strong>\uff1a\u8fd9\u4e2a\u51fd\u6570\u7528\u4e8e\u89e3\u5bc6\u5355\u4e2a PDF \u6587\u4ef6\u3002\u5982\u679c PDF \u6587\u4ef6\u88ab\u52a0\u5bc6\uff0c\u5b83\u4f1a\u5c1d\u8bd5\u53bb\u9664\u52a0\u5bc6\uff08\u4e0d\u9700\u8981\u5bc6\u7801\uff09\u3002\u7136\u540e\uff0c\u5b83\u4f1a\u5c06\u89e3\u5bc6\u540e\u7684\u5185\u5bb9\u4fdd\u5b58\u4e3a\u65b0\u7684 PDF \u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong><code>batch_decrypt_pdfs<\/code><\/strong>\uff1a\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a PDF \u6587\u4ef6\u5217\u8868\uff0c\u6279\u91cf\u89e3\u5bc6\u6bcf\u4e2a\u6587\u4ef6\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u4e3a\u65b0\u7684\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong><code>select_files<\/code><\/strong>\uff1a\u8be5\u51fd\u6570\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u6587\u4ef6\u5bf9\u8bdd\u6846\u9009\u62e9\u591a\u4e2a PDF \u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong><code>on_decrypt_button_click<\/code><\/strong>\uff1a\u5f53\u7528\u6237\u70b9\u51fb\u89e3\u5bc6\u6309\u94ae\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u83b7\u53d6\u7528\u6237\u9009\u62e9\u7684\u6587\u4ef6\u5e76\u5f00\u59cb\u89e3\u5bc6\u3002<\/li>\n\n\n\n<li><strong>GUI<\/strong>\uff1a\u4f7f\u7528 <code>Tkinter<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\uff0c\u7528\u6237\u53ef\u4ee5\u8f93\u5165\u5bc6\u7801\u5e76\u9009\u62e9\u9700\u8981\u89e3\u5bc6\u7684 PDF \u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6253\u5305 Python \u811a\u672c\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09<\/h3>\n\n\n\n<p>\u4e3a\u4e86\u8ba9\u6ca1\u6709 Python \u73af\u5883\u7684\u7528\u6237\u4e5f\u80fd\u8fd0\u884c\u8fd9\u4e2a\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u5c06 Python \u811a\u672c\u6253\u5305\u4e3a\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\uff08<code>.exe<\/code>\uff09\u3002\u6211\u4eec\u5c06\u4f7f\u7528 <code>PyInstaller<\/code> \u8fdb\u884c\u6253\u5305\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 \u5b89\u88c5 PyInstaller<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>pip install pyinstaller<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 \u6253\u5305\u811a\u672c\u4e3a\u53ef\u6267\u884c\u6587\u4ef6<\/h4>\n\n\n\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\uff0c\u5bfc\u822a\u5230\u811a\u672c\u6240\u5728\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>pyinstaller --onefile --windowed --icon=myicon.ico batch_pdf_decrypt_gui.py<br><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--onefile<\/code>\uff1a\u5c06\u6240\u6709\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u5355\u72ec\u7684 <code>.exe<\/code> \u6587\u4ef6\u3002<\/li>\n\n\n\n<li><code>--windowed<\/code>\uff1a\u9632\u6b62\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5f39\u51fa\u547d\u4ee4\u884c\u7a97\u53e3\uff08\u9002\u7528\u4e8e\u56fe\u5f62\u754c\u9762\u5e94\u7528\uff09\u3002<\/li>\n\n\n\n<li><code>--icon=myicon.ico<\/code>\uff1a\u6307\u5b9a\u56fe\u6807\u6587\u4ef6\uff08\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u56fe\u6807\uff0c\u53ef\u4ee5\u63d0\u4f9b\u56fe\u6807\u6587\u4ef6\u7684\u8def\u5f84\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 \u7ed3\u679c<\/h4>\n\n\n\n<p>\u6267\u884c\u6253\u5305\u547d\u4ee4\u540e\uff0c<code>PyInstaller<\/code> \u4f1a\u751f\u6210\u4e00\u4e2a <code>dist<\/code> \u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u6253\u5305\u597d\u7684 <code>.exe<\/code> \u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e2a <code>.exe<\/code> \u6587\u4ef6\u5206\u53d1\u7ed9\u5176\u4ed6\u7528\u6237\uff0c\u8fd0\u884c\u65f6\u4e0d\u9700\u8981 Python \u73af\u5883\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u603b\u7ed3<\/h3>\n\n\n\n<p>\u5728\u8fd9\u7bc7\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>PyPDF2<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u6279\u91cf\u89e3\u5bc6 PDF \u6587\u4ef6\u7684\u5de5\u5177\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>Tkinter<\/code> \u521b\u5efa\u4e86\u56fe\u5f62\u754c\u9762\uff0c\u65b9\u4fbf\u7528\u6237\u9009\u62e9\u6587\u4ef6\u5e76\u8f93\u5165\u5bc6\u7801\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>PyInstaller<\/code> \u5c06 Python \u811a\u672c\u6253\u5305\u4e3a\u72ec\u7acb\u7684 <code>.exe<\/code> \u6587\u4ef6\uff0c\u4fbf\u4e8e\u5728\u6ca1\u6709 Python \u73af\u5883\u7684\u8ba1\u7b97\u673a\u4e0a\u8fd0\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u5730\u89e3\u5bc6 PDF \u6587\u4ef6\uff0c\u5e76\u901a\u8fc7\u7b80\u5355\u7684\u56fe\u5f62\u754c\u9762\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u7a0b\u5e8f\u548c\u6e90\u4ee3\u7801\u6253\u5305\u4e0b\u8f7d\u5730\u5740<\/strong>\uff1a<a href=\"https:\/\/benson88.lanzn.com\/i3H0D2ocadxa\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/benson88.lanzn.com\/i3H0D2ocadxa<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u8fd9\u7bc7\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Python \u7f16 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-5509","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/5509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5509"}],"version-history":[{"count":11,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/5509\/revisions"}],"predecessor-version":[{"id":5523,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/5509\/revisions\/5523"}],"wp:attachment":[{"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benson80.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}