{"id":985106,"date":"2024-12-27T07:33:56","date_gmt":"2024-12-26T23:33:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985106.html"},"modified":"2024-12-27T07:33:58","modified_gmt":"2024-12-26T23:33:58","slug":"python%e5%a6%82%e4%bd%95%e5%8e%8b%e7%bc%a9pdf%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985106.html","title":{"rendered":"python\u5982\u4f55\u538b\u7f29pdf\u6587\u6863"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212414\/9d1f8f0c-5e11-447c-b73d-bb7e841c3c2b.webp\" alt=\"python\u5982\u4f55\u538b\u7f29pdf\u6587\u6863\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u538b\u7f29PDF\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528PyPDF2\u5e93\u538b\u7f29\u3001\u501f\u52a9Ghostscript\u5de5\u5177\u3001\u91c7\u7528PDF Compressor API\u670d\u52a1\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528PyPDF2\u5e93\u662f\u6700\u4e3a\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u4f9d\u8d56\u5916\u90e8\u5de5\u5177\u6216\u670d\u52a1\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyPDF2\u5e93\u6765\u538b\u7f29PDF\u6587\u6863\uff0c\u5e76\u5206\u6790\u5176\u5de5\u4f5c\u539f\u7406\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528PyPDF2\u5e93\u538b\u7f29PDF<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>PyPDF2\u662f\u4e00\u4e2a\u7eafPython\u7f16\u5199\u7684\u5e93\uff0c\u7528\u4e8e\u5904\u7406PDF\u6587\u4ef6\u3002\u5b83\u652f\u6301PDF\u7684\u62c6\u5206\u3001\u5408\u5e76\u3001\u52a0\u5bc6\u548c\u89e3\u5bc6\u7b49\u64cd\u4f5c\u3002\u867d\u7136PyPDF2\u4e0d\u76f4\u63a5\u63d0\u4f9b\u538b\u7f29\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u521b\u5efaPDF\u6765\u8fbe\u5230\u538b\u7f29\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5PyPDF2\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyPDF2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528PyPDF2\u8bfb\u53d6\u548c\u91cd\u5199PDF\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import PyPDF2<\/p>\n<p>def compress_pdf(input_pdf, output_pdf):<\/p>\n<p>    with open(input_pdf, &#39;rb&#39;) as file:<\/p>\n<p>        reader = PyPDF2.PdfReader(file)<\/p>\n<p>        writer = PyPDF2.PdfWriter()<\/p>\n<p>        for page in reader.pages:<\/p>\n<p>            writer.add_page(page)<\/p>\n<p>        with open(output_pdf, &#39;wb&#39;) as output_file:<\/p>\n<p>            writer.write(output_file)<\/p>\n<p>compress_pdf(&#39;input.pdf&#39;, &#39;output.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5de5\u4f5c\u539f\u7406<\/h4>\n<\/p>\n<p><p>PyPDF2\u7684\u538b\u7f29\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u901a\u8fc7\u8bfb\u53d6\u6bcf\u4e2a\u9875\u9762\u5e76\u91cd\u65b0\u5199\u5165\u6765\u5b9e\u73b0\u7684\u3002\u867d\u7136\u8fd9\u4e0d\u4f1a\u663e\u8457\u51cf\u5c11\u6587\u4ef6\u5927\u5c0f\uff0c\u4f46\u53ef\u4ee5\u53bb\u9664\u4e0d\u5fc5\u8981\u7684\u5143\u6570\u636e\u548c\u672a\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u4ece\u800c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u8fbe\u5230\u538b\u7f29\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>3. \u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6587\u4ef6\u5927\u5c0f\u4e0d\u662f\u7279\u522b\u5927\u7684PDF\u6587\u6863\uff0c\u5c24\u5176\u662f\u5f53\u60a8\u9700\u8981\u5bf9PDF\u8fdb\u884c\u5408\u5e76\u6216\u62c6\u5206\u64cd\u4f5c\u65f6\uff0cPyPDF2\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5927\u6587\u4ef6\u6216\u9700\u8981\u663e\u8457\u538b\u7f29\u7684\u6587\u4ef6\uff0c\u5efa\u8bae\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Ghostscript\u538b\u7f29PDF<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5Ghostscript<\/h4>\n<\/p>\n<p><p>Ghostscript\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406PostScript\u548cPDF\u6587\u4ef6\u7684\u5f00\u6e90\u8f6f\u4ef6\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u6548\u7684PDF\u538b\u7f29\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Ghostscript\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p>Windows\u7528\u6237\u53ef\u4ee5\u4ece<a href=\"https:\/\/www.ghostscript.com\/download\/gsdnld.html\">Ghostscript\u5b98\u7f51<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>Mac\u7528\u6237\u53ef\u4ee5\u4f7f\u7528Homebrew\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">brew install ghostscript<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>Linux\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install ghostscript<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2. \u4f7f\u7528Ghostscript\u538b\u7f29PDF<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u884c\u5bf9PDF\u8fdb\u884c\u538b\u7f29\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=\/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>-dPDFSETTINGS<\/code>\u53c2\u6570\u53ef\u4ee5\u8c03\u6574\u538b\u7f29\u8d28\u91cf\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>\/screen<\/code>\uff1a\u4f4e\u5206\u8fa8\u7387\uff0c\u9002\u5408\u5c4f\u5e55\u9605\u8bfb\u3002<\/li>\n<li><code>\/ebook<\/code>\uff1a\u4e2d\u7b49\u5206\u8fa8\u7387\uff0c\u9002\u5408\u7535\u5b50\u4e66\u3002<\/li>\n<li><code>\/printer<\/code>\uff1a\u9ad8\u5206\u8fa8\u7387\uff0c\u9002\u5408\u6253\u5370\u3002<\/li>\n<li><code>\/prepress<\/code>\uff1a\u66f4\u9ad8\u5206\u8fa8\u7387\uff0c\u9002\u5408\u51fa\u7248\u3002<\/li>\n<li><code>\/default<\/code>\uff1a\u9ed8\u8ba4\u8bbe\u7f6e\u3002<\/li>\n<\/ul>\n<p><h4>3. \u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>Ghostscript\u9002\u7528\u4e8e\u9700\u8981\u663e\u8457\u538b\u7f29\u7684PDF\u6587\u4ef6\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5305\u542b\u5927\u91cf\u56fe\u50cf\u6216\u590d\u6742\u5185\u5bb9\u7684PDF\u65f6\u6548\u679c\u663e\u8457\u3002\u5176\u7075\u6d3b\u7684\u8bbe\u7f6e\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u9700\u6c42\u8c03\u6574\u538b\u7f29\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PDF Compressor API\u670d\u52a1<\/h3>\n<\/p>\n<p><h4>1. \u9009\u62e9\u5408\u9002\u7684API\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u5728\u9009\u62e9API\u670d\u52a1\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a\u538b\u7f29\u6548\u679c\u3001\u670d\u52a1\u7a33\u5b9a\u6027\u3001\u4ef7\u683c\u548cAPI\u6613\u7528\u6027\u3002\u5e38\u89c1\u7684PDF\u538b\u7f29API\u670d\u52a1\u5305\u62ecSmallpdf\u3001PDF24\u3001PDF Compressor\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u901a\u8fc7API\u8fdb\u884c\u538b\u7f29<\/h4>\n<\/p>\n<p><p>\u4ee5Smallpdf\u4e3a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6ce8\u518c\u83b7\u5f97API\u5bc6\u94a5\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0bPython\u4ee3\u7801\u8fdb\u884c\u538b\u7f29\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def compress_pdf(api_key, input_pdf, output_pdf):<\/p>\n<p>    url = &#39;https:\/\/api.smallpdf.com\/v1\/pdf\/compress&#39;<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;Authorization&#39;: f&#39;Bearer {api_key}&#39;,<\/p>\n<p>        &#39;Content-Type&#39;: &#39;application\/pdf&#39;<\/p>\n<p>    }<\/p>\n<p>    with open(input_pdf, &#39;rb&#39;) as file:<\/p>\n<p>        response = requests.post(url, headers=headers, files={&#39;file&#39;: file})<\/p>\n<p>    with open(output_pdf, &#39;wb&#39;) as output_file:<\/p>\n<p>        output_file.write(response.content)<\/p>\n<p>compress_pdf(&#39;your_api_key&#39;, &#39;input.pdf&#39;, &#39;output.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528API\u670d\u52a1\u9002\u7528\u4e8e\u4e0d\u60f3\u5728\u672c\u5730\u5b89\u88c5\u989d\u5916\u8f6f\u4ef6\u6216\u5904\u7406\u5927\u6279\u91cf\u6587\u4ef6\u7684\u573a\u666f\u3002\u5b83\u901a\u5e38\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u538b\u7f29\u7b97\u6cd5\u548c\u66f4\u597d\u7684\u538b\u7f29\u6548\u679c\uff0c\u4f46\u9700\u8981\u8003\u8651\u7f51\u7edc\u5e26\u5bbd\u548c\u670d\u52a1\u8d39\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u538b\u7f29PDF\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528PyPDF2\u3001Ghostscript\u548cAPI\u670d\u52a1\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6587\u4ef6\u7279\u5f81\u6765\u51b3\u5b9a\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>PyPDF2<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5904\u7406\u5c0f\u6587\u4ef6\u3002<\/li>\n<li><strong>Ghostscript<\/strong>\uff1a\u5f3a\u5927\u800c\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5305\u542b\u590d\u6742\u5185\u5bb9\u7684\u5927\u6587\u4ef6\u3002<\/li>\n<li><strong>API\u670d\u52a1<\/strong>\uff1a\u65e0\u9700\u672c\u5730\u5b89\u88c5\u8f6f\u4ef6\uff0c\u9002\u5408\u6279\u91cf\u5904\u7406\u548c\u8ffd\u6c42\u9ad8\u538b\u7f29\u6548\u679c\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u6587\u4ef6\u7c7b\u578b\u548c\u538b\u7f29\u9700\u6c42\u7075\u6d3b\u7ec4\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u538b\u7f29PDF\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u538b\u7f29PDF\u6587\u4ef6\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5305\u62ecPyPDF2\u3001pdfminer\u548cpikepdf\u3002PyPDF2\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5408\u5e76\u548c\u5206\u5272PDF\u6587\u4ef6\uff0c\u800cpikepdf\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u538b\u7f29\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u60a8\u53ef\u4ee5\u8bfb\u53d6PDF\u6587\u4ef6\uff0c\u8c03\u6574\u56fe\u50cf\u8d28\u91cf\uff0c\u79fb\u9664\u4e0d\u5fc5\u8981\u7684\u5143\u7d20\uff0c\u4ece\u800c\u6709\u6548\u5730\u51cf\u5c0f\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<p><strong>\u538b\u7f29PDF\u6587\u4ef6\u540e\uff0c\u6587\u4ef6\u8d28\u91cf\u4f1a\u53d7\u5230\u5f71\u54cd\u5417\uff1f<\/strong><br \/>\u538b\u7f29PDF\u6587\u4ef6\u901a\u5e38\u4f1a\u5f71\u54cd\u6587\u4ef6\u7684\u8d28\u91cf\uff0c\u7279\u522b\u662f\u56fe\u50cf\u90e8\u5206\u3002\u5728\u4f7f\u7528\u67d0\u4e9b\u65b9\u6cd5\u538b\u7f29\u65f6\uff0c\u56fe\u50cf\u53ef\u80fd\u4f1a\u53d8\u5f97\u6a21\u7cca\u6216\u5931\u53bb\u6e05\u6670\u5ea6\u3002\u5efa\u8bae\u5728\u538b\u7f29\u6587\u4ef6\u524d\u505a\u6d4b\u8bd5\uff0c\u5c1d\u8bd5\u4e0d\u540c\u7684\u538b\u7f29\u8bbe\u7f6e\uff0c\u4ee5\u627e\u5230\u8d28\u91cf\u548c\u6587\u4ef6\u5927\u5c0f\u4e4b\u95f4\u7684\u6700\u4f73\u5e73\u8861\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1\u538b\u7f29\u540e\u7684PDF\u6587\u4ef6\u7684\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u5728\u538b\u7f29PDF\u6587\u4ef6\u540e\uff0c\u786e\u4fdd\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528PDF\u9605\u8bfb\u5668\u6253\u5f00\u538b\u7f29\u540e\u7684\u6587\u4ef6\uff0c\u68c0\u67e5\u6240\u6709\u9875\u9762\u662f\u5426\u6b63\u786e\u663e\u793a\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u5e93\u5982PyPDF2\u6765\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b8c\u6574\uff0c\u786e\u4fdd\u6240\u6709\u5bf9\u8c61\u90fd\u80fd\u6b63\u5e38\u8bbf\u95ee\uff0c\u6ca1\u6709\u635f\u574f\u7684\u9875\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u538b\u7f29PDF\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528PyPDF2\u5e93\u538b\u7f29\u3001\u501f\u52a9Ghostscript\u5de5\u5177 [&hellip;]","protected":false},"author":3,"featured_media":985113,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985106"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=985106"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985106\/revisions"}],"predecessor-version":[{"id":985117,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985106\/revisions\/985117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985113"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}