{"id":1184263,"date":"2025-01-15T19:24:30","date_gmt":"2025-01-15T11:24:30","guid":{"rendered":""},"modified":"2025-01-15T19:24:34","modified_gmt":"2025-01-15T11:24:34","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8apdf%e8%bd%ac%e4%b8%baexcel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184263.html","title":{"rendered":"python\u5982\u4f55\u628apdf\u8f6c\u4e3aexcel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133744\/7131b99a-f507-4048-bfa2-46bad582aee5.webp\" alt=\"python\u5982\u4f55\u628apdf\u8f6c\u4e3aexcel\" \/><\/p>\n<p><p> <strong>Python\u5c06PDF\u8f6c\u6362\u4e3aExcel\u7684\u65b9\u6cd5<\/strong>\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528PyPDF2\u3001pdfminer\u3001tabula-py\u7b49\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684PDF\u6587\u4ef6\u3002<strong>\u4f7f\u7528PyPDF2\u3001\u4f7f\u7528pdfminer\u3001\u4f7f\u7528tabula-py<\/strong>\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528tabula-py\u5c06PDF\u8f6c\u6362\u4e3aExcel\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528tabula-py\u5c06PDF\u8f6c\u6362\u4e3aExcel<\/strong><\/p>\n<\/p>\n<p><p>tabula-py\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u4ecePDF\u6587\u4ef6\u4e2d\u63d0\u53d6\u8868\u683c\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPandas DataFrame\u3002\u8fd9\u4e2a\u5e93\u4f9d\u8d56\u4e8eJava\u73af\u5883\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Java\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5tabula-py<\/li>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/li>\n<li>\u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c<\/li>\n<li>\u5c06\u8868\u683c\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6<\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u5b89\u88c5tabula-py<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528tabula-py\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tabula-py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Java\u73af\u5883\uff0c\u56e0\u4e3atabula-py\u4f9d\u8d56\u4e8eJava\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tabula<\/p>\n<p>import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528tabula\u7684<code>read_pdf<\/code>\u51fd\u6570\u4ecePDF\u6587\u4ef6\u4e2d\u8bfb\u53d6\u8868\u683c\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPandas DataFrame\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>example.pdf<\/code>\u7684PDF\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pdf_path = &#39;example.pdf&#39;<\/p>\n<h2><strong>\u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/strong><\/h2>\n<p>df = tabula.read_pdf(pdf_path, pages=&#39;all&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>pages=&#39;all&#39;<\/code>\u8868\u793a\u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u9875\u9762\u3002\u5982\u679c\u4f60\u53ea\u60f3\u8bfb\u53d6\u7279\u5b9a\u9875\u9762\uff0c\u53ef\u4ee5\u6307\u5b9a\u9875\u9762\u7f16\u53f7\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u8868\u683c\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u5230\u8868\u683c\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>to_excel<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06DataFrame\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6<\/p>\n<p>excel_path = &#39;output.xlsx&#39;<\/p>\n<p>df.to_excel(excel_path, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>output.xlsx<\/code>\u7684Excel\u6587\u4ef6\uff0c\u5305\u542b\u4ecePDF\u6587\u4ef6\u4e2d\u63d0\u53d6\u7684\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u8d77\uff0c\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tabula<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>PDF\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<h2><strong>\u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/strong><\/h2>\n<p>df = tabula.read_pdf(pdf_path, pages=&#39;all&#39;)<\/p>\n<h2><strong>\u5c06DataFrame\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>excel_path = &#39;output.xlsx&#39;<\/p>\n<p>df.to_excel(excel_path, index=False)<\/p>\n<p>print(f&quot;PDF\u6587\u4ef6 {pdf_path} \u5df2\u6210\u529f\u8f6c\u6362\u4e3aExcel\u6587\u4ef6 {excel_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u590d\u6742\u7684PDF\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e\u53ef\u80fd\u6bd4\u8f83\u590d\u6742\uff0c\u5bfc\u81f4\u63d0\u53d6\u6570\u636e\u65f6\u51fa\u73b0\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528tabula-py\u7684<code>read_pdf<\/code>\u51fd\u6570\u7684\u5176\u4ed6\u53c2\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u4f8b\u5982\u6307\u5b9a\u5217\u7684\u8fb9\u754c\u3001\u4f7f\u7528\u6a21\u677f\u6587\u4ef6\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e\uff0c\u5e76\u6307\u5b9a\u5217\u7684\u8fb9\u754c<\/p>\n<p>df = tabula.read_pdf(pdf_path, pages=&#39;all&#39;, guess=False, area=(50, 50, 500, 500), columns=[100, 200, 300])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>area<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u8868\u683c\u533a\u57df\u7684\u8fb9\u754c\uff0c<code>columns<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u5217\u7684\u8fb9\u754c\u3002<\/p>\n<\/p>\n<p><h3>\u5176\u4ed6Python\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86tabula-py\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9bPython\u5e93\u53ef\u4ee5\u7528\u4e8e\u5c06PDF\u8f6c\u6362\u4e3aExcel\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>PyPDF2<\/strong>\uff1a\u4e3b\u8981\u7528\u4e8e\u5904\u7406PDF\u6587\u4ef6\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u5982\u62c6\u5206\u3001\u5408\u5e76\u3001\u52a0\u5bc6\u7b49\uff0c\u4f46\u4e0d\u64c5\u957f\u63d0\u53d6\u8868\u683c\u6570\u636e\u3002<\/li>\n<li><strong>pdfminer.six<\/strong>\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684PDF\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6587\u672c\u548c\u8868\u683c\u6570\u636e\uff0c\u4f46\u4f7f\u7528\u8d77\u6765\u6bd4\u8f83\u590d\u6742\u3002<\/li>\n<li><strong>camelot-py<\/strong>\uff1a\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u4ecePDF\u4e2d\u63d0\u53d6\u8868\u683c\u6570\u636e\u7684\u5e93\uff0c\u4e0etabula-py\u7c7b\u4f3c\uff0c\u4f46\u529f\u80fd\u66f4\u5f3a\u5927\u3002<\/li>\n<\/ul>\n<p><p>\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u5c06PDF\u8f6c\u6362\u4e3aExcel\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5176\u4e2d\u4f7f\u7528tabula-py\u662f\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5b89\u88c5tabula-py\u5e93\u3001\u8bfb\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e\u3001\u5c06\u8868\u683c\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u8fd9\u4e00\u4efb\u52a1\u3002\u5bf9\u4e8e\u590d\u6742\u7684PDF\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528tabula-py\u7684\u9ad8\u7ea7\u53c2\u6570\u6765\u5904\u7406\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6PDF\u5904\u7406\u5e93\uff0c\u5982PyPDF2\u3001pdfminer.six\u548ccamelot-py\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u5176\u4ed6PDF\u5904\u7406\u5e93\u7684\u4ecb\u7ecd\u548c\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>PyPDF2<\/h4>\n<\/p>\n<p><p>PyPDF2\u662f\u4e00\u4e2a\u7eafPython\u7f16\u5199\u7684PDF\u5904\u7406\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u6267\u884c\u4e00\u4e9b\u57fa\u672c\u7684PDF\u64cd\u4f5c\uff0c\u4f8b\u5982\u8bfb\u53d6PDF\u6587\u4ef6\u3001\u5408\u5e76PDF\u6587\u4ef6\u3001\u62c6\u5206PDF\u6587\u4ef6\u3001\u65cb\u8f6c\u9875\u9762\u3001\u52a0\u5bc6\u548c\u89e3\u5bc6PDF\u6587\u4ef6\u7b49\u3002\u867d\u7136PyPDF2\u4e0d\u662f\u4e13\u95e8\u7528\u4e8e\u63d0\u53d6\u8868\u683c\u6570\u636e\u7684\uff0c\u4f46\u5b83\u5728\u5904\u7406PDF\u6587\u4ef6\u7684\u7ed3\u6784\u548c\u9875\u9762\u5185\u5bb9\u65f6\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5PyPDF2<\/li>\n<li>\u8bfb\u53d6PDF\u6587\u4ef6<\/li>\n<li>\u5408\u5e76PDF\u6587\u4ef6<\/li>\n<li>\u62c6\u5206PDF\u6587\u4ef6<\/li>\n<li>\u65cb\u8f6c\u9875\u9762<\/li>\n<\/ol>\n<p><h5>\u5b89\u88c5PyPDF2<\/h5>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5PyPDF2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyPDF2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u8bfb\u53d6PDF\u6587\u4ef6<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528PyPDF2\u8bfb\u53d6PDF\u6587\u4ef6\u5e76\u63d0\u53d6\u6587\u672c\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import PyPDF2<\/p>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<p>pdf_file = open(pdf_path, &#39;rb&#39;)<\/p>\n<p>pdf_reader = PyPDF2.PdfFileReader(pdf_file)<\/p>\n<h2><strong>\u83b7\u53d6PDF\u6587\u4ef6\u7684\u9875\u6570<\/strong><\/h2>\n<p>num_pages = pdf_reader.getNumPages()<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e00\u9875\u7684\u6587\u672c\u5185\u5bb9<\/strong><\/h2>\n<p>for page_num in range(num_pages):<\/p>\n<p>    page = pdf_reader.getPage(page_num)<\/p>\n<p>    text = page.extract_text()<\/p>\n<p>    print(f&quot;Page {page_num + 1}:\\n{text}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u5408\u5e76PDF\u6587\u4ef6<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528PyPDF2\u5408\u5e76\u591a\u4e2aPDF\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import PyPDF2<\/p>\n<p>pdf_paths = [&#39;file1.pdf&#39;, &#39;file2.pdf&#39;]<\/p>\n<p>pdf_writer = PyPDF2.PdfFileWriter()<\/p>\n<p>for pdf_path in pdf_paths:<\/p>\n<p>    pdf_file = open(pdf_path, &#39;rb&#39;)<\/p>\n<p>    pdf_reader = PyPDF2.PdfFileReader(pdf_file)<\/p>\n<p>    for page_num in range(pdf_reader.getNumPages()):<\/p>\n<p>        page = pdf_reader.getPage(page_num)<\/p>\n<p>        pdf_writer.addPage(page)<\/p>\n<p>output_path = &#39;merged.pdf&#39;<\/p>\n<p>with open(output_path, &#39;wb&#39;) as output_file:<\/p>\n<p>    pdf_writer.write(output_file)<\/p>\n<p>print(f&quot;Merged PDF saved to {output_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u62c6\u5206PDF\u6587\u4ef6<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528PyPDF2\u62c6\u5206PDF\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import PyPDF2<\/p>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<p>pdf_file = open(pdf_path, &#39;rb&#39;)<\/p>\n<p>pdf_reader = PyPDF2.PdfFileReader(pdf_file)<\/p>\n<p>for page_num in range(pdf_reader.getNumPages()):<\/p>\n<p>    pdf_writer = PyPDF2.PdfFileWriter()<\/p>\n<p>    page = pdf_reader.getPage(page_num)<\/p>\n<p>    pdf_writer.addPage(page)<\/p>\n<p>    output_path = f&#39;page_{page_num + 1}.pdf&#39;<\/p>\n<p>    with open(output_path, &#39;wb&#39;) as output_file:<\/p>\n<p>        pdf_writer.write(output_file)<\/p>\n<p>    print(f&quot;Saved page {page_num + 1} to {output_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u65cb\u8f6c\u9875\u9762<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528PyPDF2\u65cb\u8f6cPDF\u6587\u4ef6\u4e2d\u7684\u9875\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import PyPDF2<\/p>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<p>pdf_file = open(pdf_path, &#39;rb&#39;)<\/p>\n<p>pdf_reader = PyPDF2.PdfFileReader(pdf_file)<\/p>\n<p>pdf_writer = PyPDF2.PdfFileWriter()<\/p>\n<h2><strong>\u65cb\u8f6c\u6bcf\u4e00\u987590\u5ea6<\/strong><\/h2>\n<p>for page_num in range(pdf_reader.getNumPages()):<\/p>\n<p>    page = pdf_reader.getPage(page_num)<\/p>\n<p>    page.rotateClockwise(90)<\/p>\n<p>    pdf_writer.addPage(page)<\/p>\n<p>output_path = &#39;rotated.pdf&#39;<\/p>\n<p>with open(output_path, &#39;wb&#39;) as output_file:<\/p>\n<p>    pdf_writer.write(output_file)<\/p>\n<p>print(f&quot;Rotated PDF saved to {output_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>pdfminer.six<\/h4>\n<\/p>\n<p><p>pdfminer.six\u662f\u4e00\u4e2a\u5f3a\u5927\u7684PDF\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6587\u672c\u548c\u8868\u683c\u6570\u636e\u3002\u4e0ePyPDF2\u4e0d\u540c\uff0cpdfminer.six\u4e13\u6ce8\u4e8e\u6587\u672c\u63d0\u53d6\u548cPDF\u6587\u4ef6\u5185\u5bb9\u7684\u89e3\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pdfminer.six<\/li>\n<li>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6587\u672c<\/li>\n<li>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/li>\n<\/ol>\n<p><h5>\u5b89\u88c5pdfminer.six<\/h5>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5pdfminer.six\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pdfminer.six<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6587\u672c<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528pdfminer.six\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6587\u672c\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pdfminer.high_level import extract_text<\/p>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<p>text = extract_text(pdf_path)<\/p>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528pdfminer.six\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e\u9700\u8981\u66f4\u591a\u7684\u6b65\u9aa4\u548c\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528pdfminer.six\u4e2d\u7684\u5e03\u5c40\u89e3\u6790\u529f\u80fd\u6765\u63d0\u53d6\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pdfminer.high_level import extract_pages<\/p>\n<p>from pdfminer.layout import LTTextBoxHorizontal, LTFigure, LTTextLineHorizontal<\/p>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<p>table_data = []<\/p>\n<p>for page_layout in extract_pages(pdf_path):<\/p>\n<p>    for element in page_layout:<\/p>\n<p>        if isinstance(element, LTTextBoxHorizontal):<\/p>\n<p>            for text_line in element:<\/p>\n<p>                if isinstance(text_line, LTTextLineHorizontal):<\/p>\n<p>                    line_text = text_line.get_text()<\/p>\n<p>                    table_data.append(line_text.strip().split())<\/p>\n<h2><strong>\u6253\u5370\u63d0\u53d6\u7684\u8868\u683c\u6570\u636e<\/strong><\/h2>\n<p>for row in table_data:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>camelot-py<\/h4>\n<\/p>\n<p><p>camelot-py\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u4ecePDF\u4e2d\u63d0\u53d6\u8868\u683c\u6570\u636e\u7684\u5e93\uff0c\u4e0etabula-py\u7c7b\u4f3c\uff0c\u4f46\u529f\u80fd\u66f4\u5f3a\u5927\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5camelot-py<\/li>\n<li>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/li>\n<li>\u5c06\u8868\u683c\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6<\/li>\n<\/ol>\n<p><h5>\u5b89\u88c5camelot-py<\/h5>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5camelot-py\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install camelot-py[cv]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86\u4f9d\u8d56\u7684\u5e93\uff0c\u4f8b\u5982Ghostscript\u548cOpenCV\u3002<\/p>\n<\/p>\n<p><h5>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/h5>\n<\/p>\n<p><p>\u4f7f\u7528camelot-py\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPandas DataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import camelot<\/p>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<h2><strong>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/strong><\/h2>\n<p>tables = camelot.read_pdf(pdf_path, pages=&#39;all&#39;)<\/p>\n<h2><strong>\u6253\u5370\u63d0\u53d6\u7684\u8868\u683c\u6570\u636e<\/strong><\/h2>\n<p>for table in tables:<\/p>\n<p>    print(table.df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u5c06\u8868\u683c\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6<\/h5>\n<\/p>\n<p><p>\u63d0\u53d6\u5230\u8868\u683c\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>to_excel<\/code>\u51fd\u6570\u5c06\u5176\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import camelot<\/p>\n<p>import pandas as pd<\/p>\n<p>pdf_path = &#39;example.pdf&#39;<\/p>\n<h2><strong>\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u8868\u683c\u6570\u636e<\/strong><\/h2>\n<p>tables = camelot.read_pdf(pdf_path, pages=&#39;all&#39;)<\/p>\n<h2><strong>\u5c06\u6bcf\u4e2a\u8868\u683c\u4fdd\u5b58\u4e3a\u5355\u72ec\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>for i, table in enumerate(tables):<\/p>\n<p>    excel_path = f&#39;table_{i + 1}.xlsx&#39;<\/p>\n<p>    table.df.to_excel(excel_path, index=False)<\/p>\n<p>    print(f&quot;Table {i + 1} saved to {excel_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u5c06PDF\u8f6c\u6362\u4e3aExcel\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<strong>\u4f7f\u7528tabula-py<\/strong>\u662f\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684PDF\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528pdfminer.six\u548ccamelot-py\u7b49\u5e93\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u7efc\u5408\u4f7f\u7528\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u66f4\u597d\u5730\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u5c06PDF\u8f6c\u6362\u4e3aExcel\uff1f<\/strong><br \/>\u5728\u9009\u62e9PDF\u8f6cExcel\u5de5\u5177\u65f6\uff0c\u7528\u6237\u5e94\u8003\u8651\u591a\u4e2a\u56e0\u7d20\uff0c\u5982\u8f6c\u6362\u7684\u51c6\u786e\u6027\u3001\u5904\u7406\u901f\u5ea6\u548c\u53ef\u7528\u7684\u529f\u80fd\u3002\u6709\u4e9b\u5de5\u5177\u53ef\u80fd\u63d0\u4f9b\u6279\u91cf\u8f6c\u6362\u3001OCR\uff08\u5149\u5b66\u5b57\u7b26\u8bc6\u522b\uff09\u529f\u80fd\uff0c\u80fd\u591f\u5904\u7406\u626b\u63cf\u7684PDF\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u6240\u9009\u5de5\u5177\u652f\u6301\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u548c\u6587\u4ef6\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u987a\u5229\u8f6c\u6362\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684Excel\u6587\u4ef6\u683c\u5f0f\u662f\u5426\u53ef\u4ee5\u7f16\u8f91\uff1f<\/strong><br \/>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u53ef\u9760\u7684PDF\u8f6cExcel\u5de5\u5177\u8f6c\u6362\u540e\u7684Excel\u6587\u4ef6\u662f\u53ef\u4ee5\u7f16\u8f91\u7684\u3002\u7136\u800c\uff0c\u6709\u4e9b\u5de5\u5177\u53ef\u80fd\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4f1a\u5bfc\u81f4\u683c\u5f0f\u6df7\u4e71\u6216\u6570\u636e\u4e22\u5931\u3002\u5728\u8f6c\u6362\u540e\uff0c\u68c0\u67e5\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u683c\u5f0f\u5316\u60c5\u51b5\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u80fd\u591f\u987a\u5229\u4f7f\u7528\u3002<\/p>\n<p><strong>PDF\u6587\u4ef6\u4e2d\u7684\u590d\u6742\u8868\u683c\u4f1a\u5982\u4f55\u5f71\u54cd\u8f6c\u6362\u7ed3\u679c\uff1f<\/strong><br \/>\u590d\u6742\u8868\u683c\u901a\u5e38\u4f1a\u7ed9PDF\u5230Excel\u7684\u8f6c\u6362\u5e26\u6765\u6311\u6218\uff0c\u4f8b\u5982\u5408\u5e76\u5355\u5143\u683c\u3001\u5d4c\u5957\u8868\u683c\u6216\u7279\u6b8a\u683c\u5f0f\u7684\u6587\u672c\u3002\u5728\u9009\u62e9\u8f6c\u6362\u5de5\u5177\u65f6\uff0c\u5efa\u8bae\u67e5\u770b\u7528\u6237\u8bc4\u4ef7\u548c\u793a\u4f8b\uff0c\u4ee5\u4e86\u89e3\u8be5\u5de5\u5177\u5728\u5904\u7406\u590d\u6742\u8868\u683c\u65f6\u7684\u8868\u73b0\u3002\u6709\u4e9b\u5de5\u5177\u53ef\u80fd\u63d0\u4f9b\u9884\u89c8\u529f\u80fd\uff0c\u5e2e\u52a9\u7528\u6237\u5728\u5b9e\u9645\u8f6c\u6362\u524d\u8bc4\u4f30\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u6570\u636e\u5b89\u5168\uff1f<\/strong><br \/>\u5728\u8fdb\u884cPDF\u8f6c\u6362\u65f6\uff0c\u6570\u636e\u5b89\u5168\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4f7f\u7528\u4fe1\u8a89\u826f\u597d\u7684\u5de5\u5177\u548c\u670d\u52a1\uff0c\u786e\u4fdd\u5176\u5177\u5907\u826f\u597d\u7684\u9690\u79c1\u653f\u7b56\u548c\u6570\u636e\u4fdd\u62a4\u63aa\u65bd\u3002\u907f\u514d\u4f7f\u7528\u4e0d\u660e\u6765\u6e90\u7684\u5728\u7ebf\u5de5\u5177\uff0c\u4ee5\u9632\u6b62\u6f5c\u5728\u7684\u6570\u636e\u6cc4\u9732\u6216\u6ee5\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06PDF\u8f6c\u6362\u4e3aExcel\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528PyPDF2\u3001pdfminer\u3001tabula-py\u7b49\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1184274,"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\/1184263"}],"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=1184263"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184263\/revisions"}],"predecessor-version":[{"id":1184276,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184263\/revisions\/1184276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184274"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}