<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python截圖PDF,在指定區(qū)域并提取文本

          共 8499字,需瀏覽 17分鐘

           ·

          2021-01-28 17:28

          作者:小小明,「快學(xué)Pthon」專欄作者

          先說需求:PDF文件結(jié)構(gòu)都一致,對于下圖紅框區(qū)域截圖并提取文本

          測試pdfplumber庫

          先試用一下pdfplumber看看能否提取出文本

          import pdfplumber
          with pdfplumber.open("測試文檔.pdf"as p:
              page = p.pages[0]
              print(page.extract_text())

          運行結(jié)果:

          Date of Test : 2020-11-05 R
          Test Engineer : ? e
          s
          KAYSER-THREDE Contact Name : WX u
          l
          00 EVAluation Version: 2.1.7 sample.def ta
          1 n
          t
          0
          8
          Z0
          Y, 6
          X, 
          g] 40 1
          n [ . P
          o
          ati20 ag
          r
          e e
          cel  o
          ac0 f J
          071H 7
          -20 .0; Vo = 15 / 2020-11HEAD00ead Acce 822-75
          0-40 3.889 m1-0500E2ACleration -HFC
          1080 /s; M = 11 RA / CFC SP 1 Res A_202
          g]60 60 kg 1000ultant 0_11_
          t [ 0
          n 5
          ulta40  13
          s
          e _
          r0 2
          2 5
          00
          0 F
          -200 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 rid
          a
          time [ms] y
          , 6
          .1
          A1
          Analysis Interval:  0 - 1000 [ms] naly.202
          Max(61 ms) = 72 g; Min(4.3 ms) = 0.04043 g s0
          cHoICn t=.  A330m7 (s5(55.64. 6-1 6 -6 .539 m.61s )m; Hs)IC =3 665 =.7 340 g7; ( c5u5.m4 .- A 636m.3s m =s 7);0 H.1I8C g15 = 307 (55.4 - 66.3 ms) is: IA  11:2
          T3

          試用后發(fā)現(xiàn),pdfplumber提取對這種存在旋轉(zhuǎn)文字的pdf文字提取效果非常糟糕,即使是正常順序的位置,也出現(xiàn)了交錯現(xiàn)象。

          通過PyMuPDF實現(xiàn)區(qū)域截圖和區(qū)域文字提取

          官方文檔:https://pymupdf.readthedocs.io/en/latest/index.html

          Github:https://github.com/pymupdf/PyMuPDF

          安裝:

          pip install pymupdf

          截圖

          先測試截取左下角的部分:

          from IPython.display import oc = fitz.open("測試文檔.pdf")
          page = pdfDoc[0]
          mat = fitz.Matrix(11)  # 1.5表示放大1.5倍
          rect = page.rect
          clip = fitz.Rect(00.87*rect.height,
                           rect.width*0.8, rect.height)
          pix = page.getPixmap(matrix=mat, alpha=False, clip=clip)
          display(https://img-blog.csdnimg.cn/img_convert/4d9085e91f5bebc9cd34e7a25e3377fe.png)

          - fitz.Matrix\(11\)的兩個參數(shù)表示寬度和高度的放大系數(shù),上面的截的圖較小可以通過該參數(shù)放大
          - fitz.Rect有好幾種坐標模式,我選擇了\(x0, y0, x1, y1\)這種坐標模式來定位要截取的區(qū)域
          - page.getPixmap傳入放大系數(shù)和區(qū)域即可獲取圖片對象,可直接獲取圖片的數(shù)據(jù)也可以寫入到文件保存起來

          再測試截取右上角部分:

          ```python
          clip = fitz.Rect(0.8*rect.width, 0.27*rect.height,
                           rect.width*0.9, rect.height)
          pix = page.getPixmap(matrix=mat.preRotate(-90), alpha=False, clip=clip)
          display(https://img-blog.csdnimg.cn/img_convert/a759983bd8b9da691e1572f310b673c8.png)

          mat.preRotate\(-90\)實現(xiàn)了截取區(qū)域逆時針旋轉(zhuǎn)90度。

          保存圖片很簡單,只需調(diào)用write.write# 文字提取

          通過fitz.Rect要提取文字的區(qū)域即可:

          ```python
          a_text = page.getText(clip=clip)
          print(a_text)
          1. Page of J7822-75-HFCA_2020_11_05 13_25
          Head Acceleration SP 1 Resultant
          11HEAD0000E2ACRA / CFC1000
          75 / 2020-11-05
          0.0; Vo = 13.889 m/s; M = 1160 kg
          Friday, 6.11.2020  11:23
          Analysis: IAT

          這段文本提取的效果還不錯!

          再測試一下左下角部分:

          clip = fitz.Rect(00.87*rect.height,
                           rect.width*0.8, rect.height)
          b_text = page.getText(clip=clip)
          print(b_text)
          Max(61 ms) = 72 g; Min(4.3 ms) = 0.04043 g
          cont. A3ms(56.61 - 59.61 ms) = 65.74 g; cum. A3ms = 70.18 g
          HIC = 307 (55.4 - 66.3 ms); HIC36 = 307 (55.4 - 66.3 ms); HIC15 = 307 (55.4 - 66.3 ms)
          Analysis Interval:  0 - 1000 [ms]

          文本行順序處理

          文字的行順序似乎與原始圖片的文本順序不一致。不過我們可以借助pandas自定義排序,還原到一致的順序。

          import pandas as pd

          tmp = pd.DataFrame(b_text.splitlines(), columns=["a"])
          tmp["b"] = (tmp.a.str[:2]).astype("category")
          tmp.b.cat.set_categories(
              ['An''re''vi''Ma''co''VC''ES'], inplace=True)
          tmp.sort_values('b', inplace=True)
          b_text = '\n'.join(tmp.a.to_list())
          print(b_text)
          Analysis Interval:  0 - 1000 [ms]
          Max(61 ms) = 72 g; Min(4.3 ms) = 0.04043 g
          cont. A3ms(56.61 - 59.61 ms) = 65.74 g; cum. A3ms = 70.18 g
          HIC = 307 (55.4 - 66.3 ms); HIC36 = 307 (55.4 - 66.3 ms); HIC15 = 307 (55.4 - 66.3 ms)

          前5頁運行結(jié)果展示

          -------------- 1 --------------

          1. Page of J7822-75-HFCA_2020_11_05 13_25
          Head Acceleration SP 1 Resultant
          11HEAD0000E2ACRA / CFC1000
          75 / 2020-11-05
          0.0; Vo = 13.889 m/s; M = 1160 kg
          Friday, 6.11.2020  11:23
          Analysis: IAT
          Analysis Interval:  0 - 1000 [ms]
          Max(61 ms) = 72 g; Min(4.3 ms) = 0.04043 g
          cont. A3ms(56.61 - 59.61 ms) = 65.74 g; cum. A3ms = 70.18 g
          HIC = 307 (55.4 - 66.3 ms); HIC36 = 307 (55.4 - 66.3 ms); HIC15 = 307 (55.4 - 66.3 ms)

          -------------- 2 --------------

          2. Page of J7822-75-HFCA_2020_11_05 13_25
          Head Acceleration X SP 1
          11HEAD0000E2ACXA / CFC1000
          75 / 2020-11-05
          0.0; Vo = 13.889 m/s; M = 1160 kg
          Friday, 6.11.2020  11:23
          Analysis: IAT
          Analysis Interval:  0 - 1000 [ms]
          Max(65.5 ms) = 8.15 g; Min(52.2 ms) = -7.426 g

          -------------- 3 --------------

          3. Page of J7822-75-HFCA_2020_11_05 13_25
          Head Acceleration Y SP 1
          11HEAD0000E2ACYA / CFC1000
          75 / 2020-11-05
          0.0; Vo = 13.889 m/s; M = 1160 kg
          Friday, 6.11.2020  11:23
          Analysis: IAT
          Analysis Interval:  0 - 1000 [ms]
          Max(59.4 ms) = 71.87 g; Min(52 ms) = -9.89 g

          -------------- 4 --------------

          4. Page of J7822-75-HFCA_2020_11_05 13_25
          Head Acceleration Z SP 1
          11HEAD0000E2ACZA / CFC1000
          75 / 2020-11-05
          0.0; Vo = 13.889 m/s; M = 1160 kg
          Friday, 6.11.2020  11:23
          Analysis: IAT
          Analysis Interval:  0 - 1000 [ms]
          Max(56.5 ms) = 20.39 g; Min(63.6 ms) = -23.43 g

          -------------- 5 --------------

          5. Page of J7822-75-HFCA_2020_11_05 13_25
          Rib Left Upper Displacement Y SP 1
          11RIBSLEUPE2DSYC / CFC180
          75 / 2020-11-05
          0.0; Vo = 13.889 m/s; M = 1160 kg
          Friday, 6.11.2020  11:23
          Analysis: IAT
          Analysis Interval:  0 - 1000 [ms]
          Max(314.8 ms) = 0.2821 mm; Min(52.9 ms) = -33.24 mm


          我們的文章到此就結(jié)束啦,如果你喜歡今天的Python 實戰(zhàn)教程,請持續(xù)關(guān)注Python實用寶典。

          有任何問題,可以在公眾號后臺回復(fù):加群,回答相應(yīng)紅字驗證信息,進入互助群詢問。

          原創(chuàng)不易,希望你能在下面點個贊和在看支持我繼續(xù)創(chuàng)作,謝謝!

          點擊下方閱讀原文可獲得更好的閱讀體驗

          Python實用寶典 (pythondict.com)
          不只是一個寶典
          歡迎關(guān)注公眾號:Python實用寶典




          瀏覽 83
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天弄天天日 | 77777_亚洲午夜久久多人 | 国产免费无码精品视频网站 | 亚州精品一区 | 音影先锋成人资源站 |