{"id":9054,"date":"2023-12-22T16:41:11","date_gmt":"2023-12-22T08:41:11","guid":{"rendered":"\/?p=9054"},"modified":"2023-12-22T16:41:11","modified_gmt":"2023-12-22T08:41:11","slug":"python%e6%8f%90%e5%8f%96pdf%e6%96%87%e6%9c%ac%e5%86%85%e5%ae%b9-%e8%a7%a3%e5%86%b3%e5%8f%aa%e8%83%bd%e9%80%90%e8%a1%8c%e8%af%bb%e5%8f%96%ef%bc%8c%e5%be%97%e5%88%b0%e6%ae%b5%e8%90%bd","status":"publish","type":"post","link":"\/?p=9054","title":{"rendered":"python\u63d0\u53d6pdf\u6587\u672c\u5185\u5bb9\u2014\u2014\u89e3\u51b3\u53ea\u80fd\u9010\u884c\u8bfb\u53d6\uff0c\u5f97\u5230\u6bb5\u843d"},"content":{"rendered":"<p>pdf\u6bb5\u843d\u5206\u6790\uff1a<\/p>\n<p>\u4f7f\u7528pdfplumber\u5305\uff0c\u5c06\u9010\u884c\u8bfb\u53d6\u7684\u63d0\u53d6\u6210\u6bb5\u843d<\/p>\n<p>\u5c31\u4e00\u4e2a\u601d\u8def\uff1a\u6309\u9996\u884c\u7f29\u8fdb\u63d0\u53d6\u3002\u4e5f\u5c31\u662f\u53ea\u63d0\u53d6\u9996\u884c\u7f29\u8fdb\u7684\u3002<\/p>\n<p>\u5982\u9996\u884c\u9876\u683c\u7684\uff0c\u53e6\u505a\u8003\u8651\u5427\uff0c\u5148\u505a\u9996\u884c\u7f29\u8fdb\u3002<\/p>\n<p>\u4e00\u4e2a<code>char<\/code>\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-json\">{\n    &quot;matrix&quot;: [\n        0.05,\n        0.0,\n        0.0,\n        0.05,\n        122.04,\n        601.01\n    ],\n    &quot;fontname&quot;: &quot;JDKKJW+KaiTi&quot;,\n    &quot;adv&quot;: 319.0,\n    &quot;upright&quot;: True,\n    &quot;x0&quot;: 122.04,\n    &quot;y0&quot;: 598.09115,\n    &quot;x1&quot;: 137.99,\n    &quot;y1&quot;: 614.04115,\n    &quot;width&quot;: 15.950000000000003,\n    &quot;height&quot;: 15.950000000000045,\n    &quot;size&quot;: 15.950000000000045,\n    &quot;mcid&quot;: None,\n    &quot;tag&quot;: None,\n    &quot;object_type&quot;: &quot;char&quot;,\n    &quot;page_number&quot;: 1,\n    &quot;ncs&quot;: &quot;DeviceRGB&quot;,\n    &quot;text&quot;: &quot;\u7b2c&quot;,\n    &quot;stroking_color&quot;: [\n        0,\n        0,\n        0\n    ],\n    &quot;stroking_pattern&quot;: None,\n    &quot;non_stroking_color&quot;: [\n        0,\n        0,\n        0\n    ],\n    &quot;non_stroking_pattern&quot;: None,\n    &quot;top&quot;: 227.85884999999996,\n    &quot;bottom&quot;: 243.80885,\n    &quot;doctop&quot;: 227.85884999999996\n}<\/code><\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5173\u4e8ePDF\u4e2d\u6587\u672c\u5b57\u7b26\u4fe1\u606f\u7684\u89e3\u91ca\uff1a<\/p>\n<ul>\n<li>'matrix': (0.05, 0.0, 0.0, 0.05, 122.04, 601.01) - \u8fd9\u4e2a\u77e9\u9635\u8868\u793a\u5b57\u7b26\u7684\u53d8\u6362\u77e9\u9635\uff0c\u7528\u4e8e\u5728PDF\u9875\u9762\u4e0a\u5b9a\u4f4d\u5b57\u7b26\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/li>\n<li>'fontname': 'JDKKJW+KaiTi' - \u8fd9\u8868\u793a\u5b57\u7b26\u6240\u4f7f\u7528\u7684\u5b57\u4f53\u540d\u79f0\u3002<\/li>\n<li>'adv': 319.0 - \u8fd9\u662f\u5b57\u7b26\u7684\u9ad8\u5ea6\u3002<\/li>\n<li>'upright': True - \u8868\u793a\u5b57\u7b26\u662f\u76f4\u7acb\u7684\u3002<\/li>\n<li>'x0': 122.04, 'y0': 598.09115, 'x1': 137.99, 'y1': 614.04115 - \u8fd9\u4e9b\u662f\u5b57\u7b26\u8fb9\u754c\u6846\u7684\u5750\u6807\uff0c\u8868\u793a\u5b57\u7b26\u6240\u5728\u7684\u77e9\u5f62\u533a\u57df\u3002<\/li>\n<li>'width': 15.950000000000003, 'height': 15.950000000000045, 'size': 15.950000000000045 - \u8fd9\u4e9b\u662f\u5b57\u7b26\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u548c\u5927\u5c0f\u3002<\/li>\n<li>'mcid': None, 'tag': None - \u8fd9\u4e9b\u662f\u4e0ePDF\u6807\u8bb0\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u901a\u5e38\u7528\u4e8e\u7ed3\u6784\u5316PDF\u6587\u6863\u3002<\/li>\n<li>'object_type': 'char' - \u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u5bf9\u8c61\u3002<\/li>\n<li>'page_number': 1 - \u8868\u793a\u5b57\u7b26\u6240\u5728\u7684\u9875\u9762\u7f16\u53f7\u3002<\/li>\n<li>'ncs': 'DeviceRGB' - \u8868\u793a\u5b57\u7b26\u7684\u989c\u8272\u7a7a\u95f4\u3002<\/li>\n<li>'text': '\u7b2c' - \u8fd9\u662f\u5b57\u7b26\u7684\u6587\u672c\u5185\u5bb9\uff0c\u8fd9\u91cc\u662f'\u7b2c'\u5b57\u3002<\/li>\n<li>'stroking_color': (0, 0, 0), 'stroking_pattern': None, 'non_stroking_color': (0, 0, 0), 'non_stroking_pattern': None - \u8fd9\u4e9b\u662f\u5b57\u7b26\u7684\u989c\u8272\u4fe1\u606f\u3002<\/li>\n<li>'top': 227.85884999999996, 'bottom': 243.80885, 'doctop': 227.85884999999996 - \u8fd9\u4e9b\u662f\u5b57\u7b26\u6240\u5728\u7684\u77e9\u5f62\u533a\u57df\u7684\u9876\u90e8\u548c\u5e95\u90e8\u4f4d\u7f6e\uff0c\u4ee5\u53ca\u76f8\u5bf9\u4e8e\u6574\u4e2a\u6587\u6863\u9876\u90e8\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u4fe1\u606f\u63cf\u8ff0\u4e86PDF\u4e2d\u7279\u5b9a\u5b57\u7b26\u7684\u4f4d\u7f6e\u3001\u5927\u5c0f\u3001\u989c\u8272\u548c\u5176\u4ed6\u5c5e\u6027\u3002<\/p>\n<p><code>\u793a\u4f8b\u4ee3\u7801\uff1a<\/code><\/p>\n<pre><code class=\"language-python\">import re\nimport pdfplumber\nfrom configs.config import *\ndef get_text_lines(file_path):\n    &quot;&quot;&quot;\n    \u5c06pdf\u4e2d\u7684\u6587\u672c\u6309\u884c\u8bfb\u53d6\uff0c\u5e76\u8f93\u51fa\u6587\u672c+\u9996\u5b57\u7b26x\u8f74\u4f4d\u7f6e\u4fe1\u606f\u7684\u5b57\u5178\u5217\u8868\n    :param file_path: PDF\u6587\u4ef6\u8def\u5f84\n    :return:\n    &quot;&quot;&quot;\n    min_x0 = min_width = 99999\n\n    result = []\n    with pdfplumber.open(file_path) as pdf:\n        for page in pdf.pages:\n            # \u83b7\u53d6\u9875\u9762\u4e0a\u7684\u6240\u6709\u5b57\u7b26\n            chars = page.chars\n            # \u6839\u636e\u5b57\u7b26\u7684\u5782\u76f4\u4f4d\u7f6e\u5bf9\u5b83\u4eec\u8fdb\u884c\u5206\u7ec4\uff0c\u4ee5\u91cd\u65b0\u6784\u5efa\u6bb5\u843d\u7ed3\u6784\n            line = &quot;&quot;\n            x0 = 0\n            last_height = 0\n            last_top = None\n            index = 0\n            while True:\n                if index == len(chars):\n                    break\n                char = chars[index]\n                if line == &quot;&quot;:\n                    # \u5982\u679cline\u4e3a\u7a7a(\u4e0a\u4e00\u884c\u6587\u5b57\u5df2\u6dfb\u52a0\u5b8c\u6bd5)\uff0c\u5e94\u5f00\u59cb\u6dfb\u52a0char\u7684\u4f4d\u7f6e\u4fe1\u606f\n                    last_top = char[&quot;top&quot;]\n                    last_height = char[&quot;height&quot;]\n                    width = char[&quot;width&quot;]\n                    x0 = char[&quot;x0&quot;]\n                    if x0 &lt; min_x0:\n                        min_x0 = x0\n                    if min_width &gt; width:\n                        min_width = width\n                    text = char[&quot;text&quot;]\n                    line += text\n                    index += 1\n                else:\n                    # \u65e0\u9700\u6dfb\u52a0char\u7684\u4f4d\u7f6e\u4fe1\u606f\uff0c\u8865\u5145line\u7684\u6587\u672c\u5185\u5bb9\n                    if char[&#039;top&#039;] - last_top &gt; HEIGHT_COEFFICIENT * last_height or last_top - char[&#039;top&#039;] &gt; HEIGHT_COEFFICIENT * last_height: # \u5047\u8bbe\u65b0\u5b57\u7b26\u4e0e\u524d\u4e00\u4e2a\u5b57\u7b26\u4e0d\u5728\u540c\u4e00\u884c\n                        if not re.match(&#039;^\u2014[0123456789]+\u2014$&#039;, line):\n                            result.append({&quot;line&quot;: line, &quot;x0&quot;: x0})\n                        line = &quot;&quot;\n                    else:\n                        text = char[&quot;text&quot;]\n                        line += text\n                        index += 1\n            # \u6700\u540e\u4e00\u884c\uff1a\n            if not re.match(&#039;^\u2014[0123456789]+\u2014$&#039;, line):\n                result.append({&quot;line&quot;: line,&quot;x0&quot;: x0})\n    paragraph_list = []\n    indent = INDENT_COEFFICIENT * min_width\n    paragraph = &quot;&quot;\n    index = 0\n    while True:\n        if index+1 == len(result):\n            # \u6700\u540e\u4e00\u884c\u4e86\uff1a\n            if result[index][&quot;x0&quot;] - min_x0 &gt; indent:\n                # \u6700\u540e\u4e00\u884c\u662f\u7f29\u8fdb\u7684\uff1a\n                paragraph_list.append(result[index][&quot;line&quot;])\n            else:\n                paragraph += result[index][&quot;line&quot;]\n                paragraph_list.append(paragraph)\n            break\n        if result[index][&quot;x0&quot;] - min_x0 &gt; indent and result[index+1][&quot;x0&quot;] - min_x0 &gt; indent:\n            # \u5982\u679c\u8be5\u884c\u662f\u7f29\u8fdb\u7684\uff0c\u4e0b\u4e00\u884c\u4e5f\u7f29\u8fdb\uff0c\u90a3\u4e48\u8ba4\u4e3a\u662f\u6bb5\u843d\n            paragraph = result[index][&quot;line&quot;]\n            paragraph_list.append(paragraph)\n            paragraph = &quot;&quot;\n        elif result[index][&quot;x0&quot;] - min_x0 &gt; indent and result[index+1][&quot;x0&quot;] - min_x0 &lt; indent:\n            # \u5982\u679c\u8be5\u884c\u662f\u7f29\u8fdb\u7684\uff0c\u4e0b\u4e00\u884c\u4e0d\u662f\uff0c\u90a3\u4e48\u5148\u4e0d\u6dfb\u52a0\n            paragraph = result[index][&quot;line&quot;]\n        elif result[index][&quot;x0&quot;] - min_x0 &lt; indent and result[index+1][&quot;x0&quot;] - min_x0 &gt; indent:\n            # \u672c\u884c\u65e0\u7f29\u8fdb\uff0c\u4e0b\u4e00\u884c\u7f29\u8fdb\uff0c\u8ba4\u4e3a\u6bb5\u843d\u7ed3\u675f\uff0c\u6dfb\u52a0\n            paragraph += result[index][&quot;line&quot;]\n            paragraph_list.append(paragraph)\n            paragraph = &quot;&quot;\n        elif result[index][&quot;x0&quot;] - min_x0 &lt; indent and result[index+1][&quot;x0&quot;] - min_x0 &lt; indent:\n            # \u672c\u884c\u65e0\u7f29\u8fdb\uff0c\u4e0b\u4e00\u884c\u65e0\u7f29\u8fdb\uff0c\u8ba4\u4e3a\u6bb5\u843d\u672a\u7ed3\u675f\n            paragraph += result[index][&quot;line&quot;]\n        index += 1\n    return paragraph_list\nif __name__ == &#039;__main__&#039;:\n    file_path = r&#039;D:\\workspace\\\u9a71\u52a8\u53ca\u811a\u672c\\\u98ce\u63a7\u89c4\u7ae0\u5236\u5ea6\u6587\u6863\u7ed3\u6784\u5316\\\u519c\u6751\u96c6\u4f53\u7ecf\u6d4e\u7ec4\u7ec7\u8d22\u52a1\u5236\u5ea6.pdf&#039;\n    print(get_text_lines(file_path))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>pdf\u6bb5\u843d\u5206\u6790\uff1a \u4f7f\u7528pdfplumber\u5305\uff0c\u5c06\u9010\u884c\u8bfb\u53d6\u7684\u63d0\u53d6\u6210\u6bb5\u843d \u5c31\u4e00\u4e2a\u601d\u8def\uff1a\u6309\u9996\u884c\u7f29\u8fdb\u63d0\u53d6\u3002\u4e5f\u5c31\u662f\u53ea\u63d0\u53d6\u9996\u884c\u7f29\u8fdb\u7684\u3002 \u5982\u9996\u884c\u9876\u683c\u7684   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[53,48],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9054"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9054"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9054\/revisions"}],"predecessor-version":[{"id":9055,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9054\/revisions\/9055"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9054"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}