{"id":1041961,"date":"2024-12-31T12:53:32","date_gmt":"2024-12-31T04:53:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1041961.html"},"modified":"2024-12-31T12:53:35","modified_gmt":"2024-12-31T04:53:35","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96pdf%e8%ae%ba%e6%96%87%e9%87%8c%e9%9d%a2%e7%9a%84%e6%a0%87%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1041961.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6Pdf\u8bba\u6587\u91cc\u9762\u7684\u6807\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a096abda-eb41-4f07-a5ea-3b7efedba0e1.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6Pdf\u8bba\u6587\u91cc\u9762\u7684\u6807\u9898\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6Pdf\u8bba\u6587\u91cc\u9762\u7684\u6807\u9898\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528PyMuPDF\u5e93\u3001\u4f7f\u7528PyPDF2\u5e93\u3001\u4f7f\u7528PDFMiner\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528PyMuPDF\u5e93\u662f\u4e00\u79cd\u8f83\u4e3a\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecdPyMuPDF\u5e93\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PyMuPDF\u5e93<\/p>\n<\/p>\n<p><p>PyMuPDF\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u5904\u7406PDF\u548c\u5176\u4ed6\u6587\u6863\u683c\u5f0f\u3002\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u63d0\u53d6PDF\u6587\u6863\u4e2d\u7684\u6807\u9898\u548c\u5176\u4ed6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyMuPDF\u8bfb\u53d6PDF\u8bba\u6587\u6807\u9898\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5PyMuPDF\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5PyMuPDF\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyMuPDF<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bfb\u53d6PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528PyMuPDF\u8bfb\u53d6PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fitz  # PyMuPDF<\/p>\n<p>def extract_titles_from_pdf(pdf_path):<\/p>\n<p>    doc = fitz.open(pdf_path)<\/p>\n<p>    titles = []<\/p>\n<p>    for page_num in range(len(doc)):<\/p>\n<p>        page = doc.load_page(page_num)<\/p>\n<p>        blocks = page.get_text(&quot;dict&quot;)[&quot;blocks&quot;]<\/p>\n<p>        for block in blocks:<\/p>\n<p>            if block[&quot;type&quot;] == 0:  # 0 means text block<\/p>\n<p>                for line in block[&quot;lines&quot;]:<\/p>\n<p>                    for span in line[&quot;spans&quot;]:<\/p>\n<p>                        if span[&quot;flags&quot;] == 20:  # Check for bold and larger font size (common for titles)<\/p>\n<p>                            titles.append(span[&quot;text&quot;])<\/p>\n<p>    return titles<\/p>\n<p>pdf_path = &quot;path\/to\/your\/pdf_file.pdf&quot;<\/p>\n<p>titles = extract_titles_from_pdf(pdf_path)<\/p>\n<p>for title in titles:<\/p>\n<p>    print(title)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>fitz.open()<\/code>\u6253\u5f00PDF\u6587\u4ef6\uff0c\u7136\u540e\u904d\u5386\u6bcf\u4e00\u9875\u548c\u6bcf\u4e2a\u6587\u672c\u5757\uff0c\u68c0\u67e5\u6587\u672c\u5757\u7684\u5c5e\u6027\u4ee5\u8bc6\u522b\u6807\u9898\u3002\u901a\u5e38\uff0c\u6807\u9898\u4f1a\u4f7f\u7528\u8f83\u5927\u7684\u5b57\u4f53\u548c\u52a0\u7c97\u6837\u5f0f\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5<code>span[&quot;flags&quot;]<\/code>\u5c5e\u6027\u6765\u786e\u5b9a\u3002\u63d0\u53d6\u51fa\u7684\u6807\u9898\u4f1a\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PyPDF2\u5e93<\/p>\n<\/p>\n<p><p>PyPDF2\u662f\u4e00\u4e2a\u5e38\u7528\u7684Python\u5e93\uff0c\u7528\u4e8e\u5904\u7406PDF\u6587\u4ef6\u3002\u867d\u7136\u5b83\u7684\u529f\u80fd\u6ca1\u6709PyMuPDF\u90a3\u4e48\u5f3a\u5927\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u7528\u6765\u63d0\u53d6PDF\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyPDF2\u63d0\u53d6PDF\u6807\u9898\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5PyPDF2\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5PyPDF2\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyPDF2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bfb\u53d6PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528PyPDF2\u8bfb\u53d6PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import PyPDF2<\/p>\n<p>def extract_text_from_first_page(pdf_path):<\/p>\n<p>    with open(pdf_path, &quot;rb&quot;) as file:<\/p>\n<p>        reader = PyPDF2.PdfFileReader(file)<\/p>\n<p>        first_page = reader.getPage(0)<\/p>\n<p>        text = first_page.extract_text()<\/p>\n<p>    return text<\/p>\n<p>def extract_titles_from_text(text):<\/p>\n<p>    lines = text.split(&#39;\\n&#39;)<\/p>\n<p>    titles = [line for line in lines if line.isupper() or line.istitle()]<\/p>\n<p>    return titles<\/p>\n<p>pdf_path = &quot;path\/to\/your\/pdf_file.pdf&quot;<\/p>\n<p>text = extract_text_from_first_page(pdf_path)<\/p>\n<p>titles = extract_titles_from_text(text)<\/p>\n<p>for title in titles:<\/p>\n<p>    print(title)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>PdfFileReader<\/code>\u6253\u5f00PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u7b2c\u4e00\u9875\u7684\u6587\u672c\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u6587\u672c\u6309\u884c\u5206\u5272\uff0c\u5e76\u901a\u8fc7\u68c0\u67e5\u6bcf\u4e00\u884c\u662f\u5426\u4e3a\u5927\u5199\u6216\u6807\u9898\u683c\u5f0f\u6765\u8bc6\u522b\u6807\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001PDFMiner\u5e93<\/p>\n<\/p>\n<p><p>PDFMiner\u662f\u4e00\u4e2a\u7528\u4e8e\u4ecePDF\u6587\u6863\u4e2d\u63d0\u53d6\u4fe1\u606f\u7684Python\u5de5\u5177\u3002\u867d\u7136\u5b83\u6bd4\u524d\u4e24\u4e2a\u5e93\u66f4\u590d\u6742\uff0c\u4f46\u5b83\u53ef\u4ee5\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u63a7\u5236\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDFMiner\u63d0\u53d6PDF\u6807\u9898\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5PDFMiner\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5PDFMiner\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pdfminer.six<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8bfb\u53d6PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528PDFMiner\u8bfb\u53d6PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pdfminer.high_level import extract_text<\/p>\n<p>from pdfminer.layout import LAParams<\/p>\n<p>def extract_titles_from_pdf(pdf_path):<\/p>\n<p>    laparams = LAParams()<\/p>\n<p>    text = extract_text(pdf_path, laparams=laparams)<\/p>\n<p>    lines = text.split(&#39;\\n&#39;)<\/p>\n<p>    titles = [line for line in lines if line.isupper() or line.istitle()]<\/p>\n<p>    return titles<\/p>\n<p>pdf_path = &quot;path\/to\/your\/pdf_file.pdf&quot;<\/p>\n<p>titles = extract_titles_from_pdf(pdf_path)<\/p>\n<p>for title in titles:<\/p>\n<p>    print(title)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>extract_text<\/code>\u51fd\u6570\u4ecePDF\u6587\u4ef6\u4e2d\u63d0\u53d6\u6587\u672c\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u6587\u672c\u6309\u884c\u5206\u5272\uff0c\u5e76\u901a\u8fc7\u68c0\u67e5\u6bcf\u4e00\u884c\u662f\u5426\u4e3a\u5927\u5199\u6216\u6807\u9898\u683c\u5f0f\u6765\u8bc6\u522b\u6807\u9898\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4e09\u79cd\u4f7f\u7528Python\u5e93\u8bfb\u53d6PDF\u8bba\u6587\u6807\u9898\u7684\u65b9\u6cd5\uff1aPyMuPDF\u3001PyPDF2\u548cPDFMiner\u3002<strong>\u4f7f\u7528PyMuPDF\u5e93\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u4f7f\u7528\u7b80\u5355\u3002<\/strong> \u9009\u62e9\u5177\u4f53\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u60a8\u7684\u9700\u6c42\u548cPDF\u6587\u4ef6\u7684\u590d\u6742\u7a0b\u5ea6\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6PDF\u4e2d\u7684\u6587\u672c\u5185\u5bb9\uff1f<\/strong><br \/>\u4f7f\u7528Python\u53ef\u4ee5\u5229\u7528\u5e93\u5982PyPDF2\u6216pdfplumber\u6765\u63d0\u53d6PDF\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u8bfb\u53d6PDF\u6587\u4ef6\uff0c\u5e76\u5141\u8bb8\u4f60\u63d0\u53d6\u7279\u5b9a\u9875\u9762\u6216\u6574\u4e2a\u6587\u6863\u7684\u6587\u672c\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u6267\u884c<code>pip install PyPDF2<\/code>\u6216<code>pip install pdfplumber<\/code>\u3002\u63a5\u7740\uff0c\u52a0\u8f7dPDF\u6587\u4ef6\u5e76\u4f7f\u7528\u76f8\u5e94\u7684\u51fd\u6570\u63d0\u53d6\u6587\u672c\u3002<\/p>\n<p><strong>\u63d0\u53d6PDF\u6807\u9898\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u63d0\u53d6PDF\u6807\u9898\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u683c\u5f0f\u95ee\u9898\uff0c\u4f8b\u5982\u6807\u9898\u53ef\u80fd\u88ab\u5206\u6210\u591a\u884c\u6216\u5305\u542b\u989d\u5916\u7684\u7a7a\u683c\u3002\u4e3a\u4e86\u786e\u4fdd\u63d0\u53d6\u7684\u6807\u9898\u51c6\u786e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bc6\u522b\u6807\u9898\u7684\u683c\u5f0f\uff0c\u6216\u8005\u901a\u8fc7\u5206\u6790\u6587\u672c\u7684\u4f4d\u7f6e\u6765\u5224\u65ad\u54ea\u4e9b\u5185\u5bb9\u662f\u6807\u9898\u3002\u6b64\u5916\uff0c\u67d0\u4e9bPDF\u6587\u4ef6\u7684\u6587\u672c\u53ef\u80fd\u53d7\u5230\u52a0\u5bc6\u4fdd\u62a4\uff0c\u8fd9\u4f1a\u5f71\u54cd\u63d0\u53d6\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790PDF\u6587\u4ef6\uff1f<\/strong><br \/>\u9664\u4e86PyPDF2\u548cpdfplumber\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\uff0c\u5982PyMuPDF\u3001PDFMiner\u548cfitz\u7b49\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u4f18\u7f3a\u70b9\uff0cPyMuPDF\u901a\u5e38\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u56fe\u50cf\u548c\u6587\u672c\uff0c\u800cPDFMiner\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5e03\u5c40\u5206\u6790\u529f\u80fd\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u7684\u5e93\u53ef\u4ee5\u63d0\u9ad8\u89e3\u6790\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6Pdf\u8bba\u6587\u91cc\u9762\u7684\u6807\u9898\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528PyMuPDF\u5e93\u3001\u4f7f\u7528PyPDF2\u5e93\u3001\u4f7f\u7528PDFMiner [&hellip;]","protected":false},"author":3,"featured_media":1041973,"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\/1041961"}],"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=1041961"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041961\/revisions"}],"predecessor-version":[{"id":1041978,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1041961\/revisions\/1041978"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1041973"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1041961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1041961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1041961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}