{"id":223292,"date":"2024-05-11T19:16:30","date_gmt":"2024-05-11T11:16:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/223292.html"},"modified":"2024-05-11T19:16:37","modified_gmt":"2024-05-11T11:16:37","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8apdf%e6%96%87%e4%bb%b6%e8%bd%ac%e6%8d%a2%e6%88%90%e5%9b%be%e7%89%87%e6%a0%bc%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/223292.html","title":{"rendered":"python\u5982\u4f55\u628apdf\u6587\u4ef6\u8f6c\u6362\u6210\u56fe\u7247\u683c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25142935\/02ec2845-5b20-402c-8b04-fdff1aa59372.webp\" alt=\"python\u5982\u4f55\u628apdf\u6587\u4ef6\u8f6c\u6362\u6210\u56fe\u7247\u683c\u5f0f\" \/><\/p>\n<p><p>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06PDF\u6587\u4ef6\u8f6c\u6362\u6210\u56fe\u7247\u683c\u5f0f\uff0c<strong>\u4e3b\u8981\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff0c\u5982PDF2Image\u3001PyMuPDF\uff08\u4e5f\u79f0\u4e3afitz\uff09\u548cPillow\uff08PIL\u7684\u5347\u7ea7\u7248\uff09\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u529f\u80fd\u548c\u9009\u9879\u6765\u8c03\u6574\u8f6c\u6362\u8fc7\u7a0b\u548c\u8f93\u51fa\u56fe\u7247\u8d28\u91cf\u3002\u5176\u4e2d\uff0c<strong>PDF2Image<\/strong> \u901a\u5e38\u662f\u6700\u76f4\u63a5\u3001\u6613\u7528\u7684\u9009\u62e9\u3002\u5b83\u662f\u4f7f\u7528Poppler\u5de5\u5177\u96c6\uff0c\u53ef\u4ee5\u5c06PDF\u9875\u9762\u9010\u4e2a\u8f6c\u6362\u4e3a\u4e0d\u540c\u5206\u8fa8\u7387\u548c\u683c\u5f0f\u7684\u56fe\u7247\u3002\u4f60\u53ea\u9700\u5b89\u88c5\u8be5\u5e93\u4e14\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u6709Poppler\u5de5\u5177\u96c6\uff0c\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u811a\u672c\u6765\u5904\u7406\u8f6c\u6362\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u548c\u5de5\u5177\u96c6<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8981\u5c06PDF\u8f6c\u6362\u6210\u56fe\u7247\uff0c\u9996\u5148\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Poppler\u5e76\u4e14\u5176\u53ef\u6267\u884c\u6587\u4ef6\u5728\u7cfb\u7edf\u7684PATH\u4e2d\u3002\u7136\u540e\u901a\u8fc7pip\u5b89\u88c5PDF2Image\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pdf2image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PDF2Image\u8fdb\u884c\u8f6c\u6362<\/p>\n<\/p>\n<p><p>PDF2Image\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>convert_from_path<\/code>\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5c06PDF\u6587\u6863\u8f6c\u6362\u4e3a\u56fe\u50cf\u5217\u8868\uff0c\u6bcf\u9875\u6587\u6863\u5bf9\u5e94\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pdf2image import convert_from_path<\/p>\n<h2><strong>\u5c06PDF\u6587\u4ef6\u8f6c\u6362\u4e3a\u56fe\u7247\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868PDF\u4e2d\u7684\u4e00\u9875<\/strong><\/h2>\n<p>images = convert_from_path(&#039;\/path\/to\/your\/pdf\/file.pdf&#039;)<\/p>\n<h2><strong>\u904d\u5386\u5217\u8868\u5e76\u4fdd\u5b58\u6bcf\u4e00\u9875\u4f5c\u4e3a\u56fe\u7247<\/strong><\/h2>\n<p>for i, image in enumerate(images):<\/p>\n<p>    image.save(f&#039;page_{i}.jpg&#039;, &#039;JPEG&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PyMuPDF\u8fdb\u884c\u9ad8\u7ea7\u8f6c\u6362<\/p>\n<\/p>\n<p><p>PyMuPDF\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5e7f\u6cdb\u7684PDF\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u63d0\u53d6\u6587\u672c\u3001\u56fe\u50cf\u3001\u6e32\u67d3PDF\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyMuPDF<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884cPDF\u5230\u56fe\u7247\u7684\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fitz  # PyMuPDF<\/p>\n<h2><strong>\u6253\u5f00PDF\u6587\u4ef6<\/strong><\/h2>\n<p>pdf = fitz.open(&#039;\/path\/to\/your\/pdf\/file.pdf&#039;)<\/p>\n<h2><strong>\u904d\u5386PDF\u4e2d\u7684\u6bcf\u4e00\u9875<\/strong><\/h2>\n<p>for page_num in range(len(pdf)):<\/p>\n<p>    page = pdf.load_page(page_num)<\/p>\n<p>    # \u8bbe\u7f6e\u8f6c\u6362\u7684\u5206\u8fa8\u7387\uff0c\u8d8a\u9ad8\u56fe\u7247\u8d8a\u6e05\u6670<\/p>\n<p>    zoom_x = 2.0  # \u6c34\u5e73\u65b9\u5411\u7684\u7f29\u653e\u7cfb\u6570<\/p>\n<p>    zoom_y = 2.0  # \u5782\u76f4\u65b9\u5411\u7684\u7f29\u653e\u7cfb\u6570<\/p>\n<p>    mat = fitz.Matrix(zoom_x, zoom_y)<\/p>\n<p>    # \u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684Pixmap\u5bf9\u8c61\uff0c\u5373\u6e32\u67d3\u9875\u9762\u751f\u6210\u56fe\u50cf<\/p>\n<p>    pix = page.get_pixmap(matrix=mat)<\/p>\n<p>    # \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p>    pix.save(f&#039;page_{page_num}.png&#039;)<\/p>\n<h2><strong>\u5173\u95edPDF\u6587\u6863<\/strong><\/h2>\n<p>pdf.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f18\u5316\u548c\u5904\u7406\u8f93\u51fa\u56fe\u7247<\/p>\n<\/p>\n<p><p>\u5728\u5c06PDF\u8f6c\u6362\u4e3a\u56fe\u7247\u540e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5bf9\u8f93\u51fa\u7684\u56fe\u7247\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002Pillow\u5e93\u53ef\u4ee5\u5e2e\u4f60\u8c03\u6574\u56fe\u7247\u5927\u5c0f\u3001\u683c\u5f0f\u6216\u8d28\u91cf\u3002\u4e0b\u9762\u7ed9\u51fa\u7b80\u5355\u7684\u538b\u7f29\u56fe\u50cf\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>original_image = Image.open(&#039;page_0.jpg&#039;)<\/p>\n<p>compressed_image = original_image.resize((1024, 768), Image.ANTIALIAS)<\/p>\n<p>compressed_image.save(&#039;page_0_compressed.jpg&#039;, &#039;JPEG&#039;, quality=85)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408Python\u811a\u672c\u5316<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u8fd9\u4e00\u8fc7\u7a0b\u66f4\u52a0\u81ea\u52a8\u5316\u548c\u9002\u5e94\u4e0d\u540c\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u53ef\u5c06\u4ee5\u4e0a\u6b65\u9aa4\u6574\u5408\u5165\u4e00\u4e2aPython\u811a\u672c\u4e2d\uff0c\u5b9e\u73b0\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u8bfb\u53d6\u8f93\u5165\u6587\u4ef6\uff0c\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\u5939\u548c\u56fe\u7247\u683c\u5f0f\u7b49\u529f\u80fd\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u80fd\u591f\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\u6279\u91cf\u5904\u7406\u591a\u4e2aPDF\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5c06PDF\u6587\u4ef6\u8f6c\u6362\u4e3a\u56fe\u7247\u662fPython\u5728\u6587\u4ef6\u5904\u7406\u9886\u57df\u7684\u4e00\u4e2a\u5e38\u89c1\u4efb\u52a1\u3002\u5f97\u76ca\u4e8e\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u8fd9\u4e00\u8fc7\u7a0b\u53ef\u4ee5\u53d8\u5f97\u65e2\u7b80\u5355\u53c8\u7075\u6d3b\uff0c\u9002\u5e94\u591a\u79cd\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u7ec4\u5408\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u548c\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u5236\u5b9a\u51fa\u7b26\u5408\u7279\u5b9a\u9700\u6c42\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5c06PDF\u6587\u4ef6\u8f6c\u6362\u4e3a\u56fe\u50cf\u683c\u5f0f\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p>Q\uff1aPython\u6709\u4ec0\u4e48\u5e93\u53ef\u4ee5\u7528\u6765\u5c06PDF\u6587\u4ef6\u8f6c\u6362\u4e3a\u56fe\u50cf\u683c\u5f0f\uff1f<\/p>\n<ul>\n<li>A\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528PyPDF2\u5e93\u6765\u8bfb\u53d6PDF\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u4f7f\u7528Pillow\uff08PIL\uff09\u5e93\u5c06\u6bcf\u4e2a\u9875\u9762\u8f6c\u6362\u4e3a\u56fe\u50cf\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Q\uff1a\u5982\u4f55\u5b89\u88c5PyPDF2\u548cPillow\u5e93\uff1f<\/p>\n<ul>\n<li>A\uff1a\u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u8fd0\u884cpip install PyPDF2\u548cpip install Pillow\u6765\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Q\uff1a\u5982\u4f55\u4f7f\u7528PyPDF2\u5e93\u8bfb\u53d6PDF\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/p>\n<ul>\n<li>A\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528PyPDF2\u5e93\u4e2d\u7684PdfReader\u51fd\u6570\u6765\u6253\u5f00\u5e76\u8bfb\u53d6PDF\u6587\u4ef6\u3002\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u9875\u9762\uff0c\u5e76\u4f7f\u7528extract_text\u51fd\u6570\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Q\uff1a\u5982\u4f55\u4f7f\u7528Pillow\u5e93\u5c06PDF\u9875\u9762\u8f6c\u6362\u4e3a\u56fe\u50cf\u683c\u5f0f\uff1f<\/p>\n<ul>\n<li>A\uff1a\u4f7f\u7528Pillow\u5e93\u4e2d\u7684Image\u51fd\u6570\uff0c\u53ef\u4ee5\u6253\u5f00\u6bcf\u4e2aPDF\u9875\u9762\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u56fe\u50cf\u683c\u5f0f\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u56fe\u50cf\u4fdd\u5b58\u4e3a\u6240\u9700\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u5982JPEG\u6216PNG\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Q\uff1a\u5982\u4f55\u5c06\u591a\u4e2a\u56fe\u50cf\u6587\u4ef6\u5408\u5e76\u6210\u4e00\u4e2a\u5355\u72ec\u7684PDF\u6587\u4ef6\uff1f<\/p>\n<ul>\n<li>A\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528PyPDF2\u5e93\u4e2d\u7684PdfWriter\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PDF\u6587\u4ef6\uff0c\u5e76\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u56fe\u50cf\u6587\u4ef6\uff0c\u4f7f\u7528addPage\u51fd\u6570\u5c06\u6bcf\u4e2a\u56fe\u50cf\u6587\u4ef6\u6dfb\u52a0\u5230PDF\u4e2d\u3002\u6700\u540e\uff0c\u4f7f\u7528write\u51fd\u6570\u4fdd\u5b58\u5e76\u5173\u95edPDF\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>Q\uff1a\u5982\u4f55\u8c03\u6574\u56fe\u50cf\u7684\u5927\u5c0f\u548c\u8d28\u91cf\uff1f<\/p>\n<ul>\n<li>A\uff1a\u4f7f\u7528Pillow\u5e93\u4e2d\u7684resize\u51fd\u6570\uff0c\u53ef\u4ee5\u8c03\u6574\u56fe\u50cf\u7684\u5927\u5c0f\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6240\u9700\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u6765\u8bbe\u7f6e\u8c03\u6574\u540e\u7684\u56fe\u50cf\u5927\u5c0f\u3002\u800c\u8d28\u91cf\u65b9\u9762\uff0c\u53ef\u4ee5\u4f7f\u7528save\u51fd\u6570\u7684\u53c2\u6570\u6765\u8bbe\u7f6e\u56fe\u50cf\u4fdd\u5b58\u7684\u8d28\u91cf\u7ea7\u522b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06PDF\u6587\u4ef6\u8f6c\u6362\u6210\u56fe\u7247\u683c\u5f0f\uff0c\u4e3b\u8981\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982PDF2Image\u3001PyMuPDF [&hellip;]","protected":false},"author":3,"featured_media":223305,"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\/223292"}],"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=223292"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/223292\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/223305"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=223292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=223292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=223292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}