{"id":221836,"date":"2024-05-11T18:46:40","date_gmt":"2024-05-11T10:46:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/221836.html"},"modified":"2024-05-11T18:46:46","modified_gmt":"2024-05-11T10:46:46","slug":"python%e5%a6%82%e4%bd%95%e5%ae%8c%e6%95%b4%e7%9a%84%e5%b0%86pdf%e8%bd%ac%e6%88%90word-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/221836.html","title":{"rendered":"Python\u5982\u4f55\u5b8c\u6574\u7684\u5c06PDF\u8f6c\u6210Word"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25141749\/8069617d-7b49-4d38-925a-2416e44c0c88.webp\" alt=\"Python\u5982\u4f55\u5b8c\u6574\u7684\u5c06PDF\u8f6c\u6210Word\" \/><\/p>\n<p><p>\u5c06PDF\u8f6c\u6362\u6210Word\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u4e13\u95e8\u7684\u8f6c\u6362\u8f6f\u4ef6\u3001\u5728\u7ebf\u670d\u52a1\u3001\u6216\u7f16\u5199\u811a\u672c\u7b49\u3002<strong>\u7a0b\u5e8f\u5316\u5730\u4f7f\u7528Python\u5b9e\u73b0PDF\u5230Word\u7684\u8f6c\u6362\u6d89\u53ca\u5230\u5e93\u7684\u9009\u62e9\uff0c\u6587\u672c\u63d0\u53d6\u7684\u51c6\u786e\u6027\u3001\u683c\u5f0f\u548c\u5e03\u5c40\u7684\u518d\u73b0\u3001\u56fe\u7247\u548c\u5176\u4ed6\u5a92\u4f53\u5185\u5bb9\u7684\u5904\u7406\u7b49\u5173\u952e\u73af\u8282\u3002<\/strong> \u5176\u4e2d\uff0c<strong>PyPDF2\u548cpython-docx<\/strong> \u662f\u4e24\u4e2a\u5e38\u7528\u4e8e\u5904\u7406PDF\u548cWord\u6587\u4ef6\u7684Python\u5e93\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u6700\u5173\u952e\u7684\u662f\u6587\u672c\u7684\u63d0\u53d6\u548c\u683c\u5f0f\u8bbe\u7f6e\uff0c\u8fd9\u56e0\u4e3aPDF\u5f80\u5f80\u88ab\u8bbe\u8ba1\u4e3a\u4e00\u79cd\u300c\u7ec8\u7aef\u683c\u5f0f\u300d(end format)\uff0c\u7528\u4e8e\u5c55\u793a\u6700\u7ec8\u7684\u6253\u5370\u7248\u9762\uff0c\u800c\u4e0d\u662f\u4f5c\u4e3a\u5185\u5bb9\u7ee7\u7eed\u7f16\u8f91\u7684\u8d77\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PDF\u6587\u672c\u63d0\u53d6\u4e0e\u89e3\u6790<\/p>\n<p>PDF\u6587\u672c\u7684\u63d0\u53d6\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2aPython\u5e93\u6765\u5b8c\u6210\uff0c\u5982PyPDF2\u3001PDFMiner\u7b49\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pypdf2<\/p>\n<p>pip install pdfminer.six<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\u4f7f\u7528PyPDF2\u5e93<\/p>\n<p>PyPDF2\u4e3b\u8981\u7528\u4e8e\u5904\u7406PDF\u6587\u6863\u7684\u9875\u9762\u63d0\u53d6\u3001\u5408\u5e76\u3001\u5206\u5272\u4ee5\u53ca\u8f6c\u6362\u7b49\u4efb\u52a1\u3002\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u63d0\u53d6PDF\u4e2d\u7684\u6587\u672c\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">from PyPDF2 import PdfReader<\/p>\n<p>reader = PdfReader(&quot;example.pdf&quot;)<\/p>\n<p>text = &quot;&quot;<\/p>\n<p>for page in reader.pages:<\/p>\n<p>    text += page.extract_text() + &quot;\\n&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u800c\uff0cPyPDF2\u5728\u5904\u7406\u6587\u672c\u5c42\u53e0\u3001\u5e03\u5c40\u548c\u683c\u5f0f\u4e0a\u6709\u5c40\u9650\u6027\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528PDFMiner.six<\/p>\n<p>PDFMiner.six\u662fPDFMiner\u7684\u4e00\u4e2a\u5206\u652f\u7248\u672c\uff0c\u4f18\u5316\u4e86Python 3\u7684\u652f\u6301\uff0c\u5e76\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406PDF\u4e2d\u7684\u5e03\u5c40\u548c\u683c\u5f0f\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">from pdfminer.high_level import extract_text<\/p>\n<p>text = extract_text(&quot;example.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>PDFMiner.six\u63d0\u4f9b\u66f4\u4e3a\u7ec6\u81f4\u7684API\u7528\u6765\u83b7\u53d6\u9875\u9762\u5e03\u5c40\u4fe1\u606f\uff0c\u4ece\u800c\u5728\u53ef\u80fd\u7684\u8303\u56f4\u5185\u4fdd\u6301\u539f\u59cb\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5c06\u6587\u672c\u5199\u5165Word\u6587\u6863<\/p>\n<p>\u5904\u7406\u5b8cPDF\u6587\u672c\u63d0\u53d6\u540e\uff0c\u9700\u8981\u5c06\u63d0\u53d6\u51fa\u7684\u6587\u672c\u548c\u76f8\u5173\u683c\u5f0f\u5199\u5165Word\u6587\u6863\u3002\u8fd9\u4e00\u4efb\u52a1\u901a\u5e38\u4f7f\u7528<strong>python-docx<\/strong>\u5e93\u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u5e76\u4f7f\u7528python-docx<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">pip install python-docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c06\u63d0\u53d6\u7684\u6587\u672c\u5199\u5165Word\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<p>document = Document()<\/p>\n<p>document.add_paragraph(text)<\/p>\n<p>document.save(&quot;converted.docx&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u8f6c\u6362\u8fc7\u7a0b\u4fdd\u6301\u4e0d\u4e86PDF\u7684\u539f\u6709\u683c\u5f0f\u3002\u8981\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u683c\u5f0f\u8bbe\u5b9a\uff0c\u9700\u8981\u8fdb\u884c\u9010\u6bb5\u843d\u6216\u9010\u53e5\u7684\u683c\u5f0f\u5206\u6790\u548c\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u683c\u5f0f\u5316\u5904\u7406\u548c\u5e03\u5c40\u91cd\u5efa<\/p>\n<p>Python-docx\u5e93\u80fd\u591f\u8fdb\u884c\u8f83\u4e3a\u7ec6\u81f4\u7684\u683c\u5f0f\u548c\u6837\u5f0f\u8bbe\u5b9a\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">from docx.shared import Pt<\/p>\n<p>from docx.enum.text import WD_ALIGN_PARAGRAPH<\/p>\n<p>for paragraph in text.split(&#039;\\n&#039;):<\/p>\n<p>    p = document.add_paragraph()<\/p>\n<p>    run = p.add_run(paragraph)<\/p>\n<p>    run.font.size = Pt(12)<\/p>\n<p>    p.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u663e\u793a\u4e86\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002\u66f4\u8fdb\u4e00\u6b65\uff0c\u5f00\u53d1\u8005\u9700\u8981\u6839\u636e\u63d0\u53d6\u51fa\u7684\u683c\u5f0f\u4fe1\u606f\uff08\u6bd4\u5982\u659c\u4f53\u3001\u7c97\u4f53\u3001\u9879\u76ee\u7b26\u53f7\u5217\u8868\u7b49\uff09\uff0c\u9010\u4e2a\u5e94\u7528\u5230Word\u6587\u6863\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u56fe\u7247\u548c\u591a\u5a92\u4f53\u5185\u5bb9\u5904\u7406<\/p>\n<p>PDF\u6587\u4ef6\u4e2d\u5f80\u5f80\u5305\u542b\u56fe\u7247\u7b49\u591a\u5a92\u4f53\u5185\u5bb9\uff0cPython\u7684<strong>pdf2image<\/strong>\u5e93\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6PDF\u4e2d\u7684\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pdf2image<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">pip install pdf2image<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528pdf2image\u63d0\u53d6\u56fe\u50cf\u5e76\u4fdd\u5b58<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pdf2image import convert_from_path<\/p>\n<p>images = convert_from_path(&#039;example.pdf&#039;)<\/p>\n<p>for i, image in enumerate(images):<\/p>\n<p>    image.save(f&#039;page_{i}.png&#039;, &#039;PNG&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e4b\u540e\uff0c\u8fd9\u4e9b\u4fdd\u5b58\u4e0b\u6765\u7684\u56fe\u50cf\u53ef\u4ee5\u6309\u7167\u539f\u6709\u7684\u987a\u5e8f\u63d2\u5165\u5230Word\u6587\u6863\u4e2d\uff0c\u5b8c\u6210\u591a\u5a92\u4f53\u5185\u5bb9\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u590d\u5408\u6587\u6863\u7684\u5904\u7406<\/p>\n<p>\u5f53\u5904\u7406\u5305\u542b\u6587\u672c\u3001\u56fe\u50cf\u548c\u590d\u6742\u683c\u5f0f\u7684PDF\u6587\u6863\u65f6\uff0c\u4e00\u4e2a\u53ef\u80fd\u7684\u7b56\u7565\u662f\u5c06\u9875\u9762\u89c6\u4e3a\u56fe\u50cf\u5904\u7406\uff0c\u7136\u540e\u5c06\u56fe\u50cf\u63d2\u5165Word\u6587\u6863\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u80fd\u4fdd\u6301\u539f\u6709\u7684\u5e03\u5c40\uff0c\u4f46\u4e27\u5931\u4e86\u6587\u6863\u7684\u6587\u672c\u7f16\u8f91\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8f6c\u6362\u8d28\u91cf\u7684\u4f18\u5316\u548c\u6d4b\u8bd5<\/p>\n<p>\u4e3a\u4e86\u8fbe\u5230\u7406\u60f3\u7684\u8f6c\u6362\u8d28\u91cf\uff0c\u901a\u5e38\u9700\u8981\u53cd\u590d\u6d4b\u8bd5\u548c\u8c03\u6574\u63d0\u53d6\u811a\u672c\u7684\u53c2\u6570\u548c\u6587\u6863\u5199\u5165\u7684\u683c\u5f0f\u8bbe\u7f6e\u3002<strong>\u8f6c\u6362\u7684\u8d28\u91cf\u5f80\u5f80\u53d6\u51b3\u4e8e\u539fPDF\u7684\u8d28\u91cf\u3001\u590d\u6742\u5ea6\u4ee5\u53ca\u6240\u4f7f\u7528\u5e93\u7684\u80fd\u529b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8fdb\u884cPDF\u5230Word\u7684\u8f6c\u6362\u662f\u53ef\u884c\u7684\uff0c\u4f46\u9700\u8981\u6ce8\u610f\uff0c\u8fd9\u4e00\u4efb\u52a1\u5728\u81ea\u52a8\u5316\u7a0b\u5ea6\u3001\u683c\u5f0f\u4fdd\u6301\u548c\u8f6c\u6362\u6548\u679c\u4e0a\u53ef\u80fd\u65e0\u6cd5\u4e0e\u4e13\u95e8\u7684\u8f6c\u6362\u5de5\u5177\u76f8\u5ab2\u7f8e\u3002\u4f46\u5bf9\u4e8e\u5927\u89c4\u6a21\u6587\u6863\u5904\u7406\u6216\u8005\u81ea\u5b9a\u4e49\u8f6c\u6362\u8fc7\u7a0b\uff0c\u7f16\u5199Python\u811a\u672c\u5177\u6709\u4e0d\u53ef\u6bd4\u62df\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u5c06PDF\u8f6c\u6362\u4e3aWord\uff1f<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u4e00\u4e2a\u540d\u4e3aPyPDF2\u7684Python\u5e93\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5904\u7406PDF\u6587\u4ef6\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f7f\u7528PyPDF2\u5e93\u6253\u5f00PDF\u6587\u4ef6\uff0c\u5e76\u6309\u9875\u8bfb\u53d6\u5185\u5bb9\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python-docx\u5e93\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\u5e76\u5c06\u8bfb\u53d6\u7684PDF\u5185\u5bb9\u9010\u9875\u6dfb\u52a0\u5230Word\u4e2d\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4fdd\u5b58Word\u6587\u6863\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><strong>2. \u6709\u6ca1\u6709\u5176\u4ed6Python\u5e93\u53ef\u4ee5\u5c06PDF\u8f6c\u6362\u4e3aWord\u6587\u6863\uff1f<\/strong><\/p>\n<ul>\n<li>\u662f\u7684\uff0c\u9664\u4e86PyPDF2\u548cPython-docx\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9bPython\u5e93\u53ef\u4ee5\u5b9e\u73b0\u5c06PDF\u8f6c\u6362\u4e3aWord\u6587\u6863\u7684\u529f\u80fd\uff0c\u4f8b\u5982pdf2docx\u548cpdftotext\u7b49\u3002<\/li>\n<li>pdf2docx\u5e93\u53ef\u4ee5\u76f4\u63a5\u5c06PDF\u8f6c\u6362\u4e3aWord\u6587\u6863\uff0c\u5e76\u4e14\u652f\u6301\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u4fdd\u7559PDF\u7684\u5e03\u5c40\u3001\u8868\u683c\u7b49\u3002<\/li>\n<li>pdftotext\u5e93\u53ef\u4ee5\u5c06PDF\u8f6c\u6362\u4e3a\u6587\u672c\u683c\u5f0f\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528Python-docx\u5e93\u5c06\u6587\u672c\u6dfb\u52a0\u5230\u65b0\u7684Word\u6587\u6863\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>3. \u80fd\u5426\u5b9e\u73b0\u5c06\u5305\u542b\u56fe\u7247\u7684PDF\u8f6c\u6362\u4e3aWord\u6587\u6863\uff1f<\/strong><\/p>\n<ul>\n<li>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u7684pdf2image\u5e93\u6765\u5b9e\u73b0\u5c06\u5305\u542b\u56fe\u7247\u7684PDF\u8f6c\u6362\u4e3aWord\u6587\u6863\u3002<\/li>\n<li>\u9996\u5148\uff0c\u4f7f\u7528pdf2image\u5e93\u5c06PDF\u7684\u6bcf\u4e2a\u9875\u9762\u8f6c\u6362\u4e3a\u56fe\u7247\u683c\u5f0f\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python-docx\u5e93\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Word\u6587\u6863\uff0c\u5e76\u5c06\u8f6c\u6362\u7684\u56fe\u7247\u9010\u9875\u6dfb\u52a0\u5230Word\u6587\u6863\u4e2d\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4fdd\u5b58Word\u6587\u6863\u5e76\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u5c06PDF\u8f6c\u6362\u6210Word\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u4e13\u95e8\u7684\u8f6c\u6362\u8f6f\u4ef6\u3001\u5728\u7ebf\u670d\u52a1\u3001\u6216\u7f16\u5199\u811a\u672c\u7b49\u3002\u7a0b\u5e8f\u5316\u5730\u4f7f\u7528P [&hellip;]","protected":false},"author":3,"featured_media":221845,"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\/221836"}],"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=221836"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/221836\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/221845"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=221836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=221836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=221836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}