對(duì)pdf指定區(qū)域截圖并提取文字

      網(wǎng)友投稿 674 2025-03-31

      明鏡本清凈,明鏡亦非臺(tái)。

      本來(lái)無(wú)一物,何處惹塵埃!

      截圖PDF指定區(qū)域并提取文件

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

      測(cè)試pdfplumber庫(kù)

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

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

      1

      2

      3

      4

      運(yùn)行結(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

      1

      2

      3

      4

      5

      6

      7

      對(duì)pdf指定區(qū)域截圖并提取文字

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

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

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

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

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

      安裝:

      pip install pymupdf

      1

      截圖

      先測(cè)試截取左下角的部分:

      from IPython.display import Image import fitz pdfDoc = fitz.open("測(cè)試文檔.pdf") page = pdfDoc[0] mat = fitz.Matrix(1, 1) # 1.5表示放大1.5倍 rect = page.rect clip = fitz.Rect(0, 0.87*rect.height, rect.width*0.8, rect.height) pix = page.getPixmap(matrix=mat, alpha=False, clip=clip) display(Image(pix.getImageData()))

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      fitz.Matrix(1, 1)的兩個(gè)參數(shù)表示寬度和高度的放大系數(shù),上面的截的圖較小可以通過(guò)該參數(shù)放大

      fitz.Rect有好幾種坐標(biāo)模式,我選擇了(x0, y0, x1, y1)這種坐標(biāo)模式來(lái)定位要截取的區(qū)域

      page.getPixmap傳入放大系數(shù)和區(qū)域即可獲取圖片對(duì)象,可直接獲取圖片的數(shù)據(jù)也可以寫入到文件保存起來(lái)

      再測(cè)試截取右上角部分:

      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(Image(pix.getImageData()))

      1

      2

      3

      4

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

      保存圖片很簡(jiǎn)單,只需調(diào)用writeImage即可:

      pix.writeImage("tmp.png")

      1

      文字提取

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

      a_text = page.getText(clip=clip) print(a_text)

      1

      2

      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

      1

      2

      3

      4

      5

      6

      7

      這段文本提取的效果還不錯(cuò)!

      再測(cè)試一下左下角部分:

      clip = fitz.Rect(0, 0.87*rect.height, rect.width*0.8, rect.height) b_text = page.getText(clip=clip) print(b_text)

      1

      2

      3

      4

      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]

      1

      2

      3

      4

      文本行順序處理

      文字的行順序似乎與原始圖片的文本順序不一致。不過(guò)我們可以借助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)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      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)

      1

      2

      3

      4

      完整代碼

      import fitz # pip install PyMuPDF import os from IPython.display import Image import pandas as pd pdf_path = "測(cè)試文檔.pdf" if not os.path.exists("imgs"): os.mkdir("imgs") result = [] with fitz.open(pdf_path) as pdfDoc: for i in range(pdfDoc.pageCount): page_num = i+1 print("--------------", page_num, "--------------") page = pdfDoc[i] mat = fitz.Matrix(1.3, 1.3) # 1.5表示放大1.5倍 rect = page.rect clip = fitz.Rect(0.8*rect.width, 0.27*rect.height, rect.width*0.9, rect.height) # 想要截取的區(qū)域 pix = page.getPixmap(matrix=mat.preRotate(-90), alpha=False, clip=clip) # 將頁(yè)面轉(zhuǎn)換為圖像 pix.writeImage(f"imgs/{page_num}_a.png") img1 = pix.getImageData() display(Image(img1)) a_text = page.getText(clip=clip) print(a_text) clip = fitz.Rect(0, 0.87*rect.height, rect.width*0.8, rect.height) pix = page.getPixmap(matrix=mat.preRotate(90), alpha=False, clip=clip) pix.writeImage(f"imgs/{page_num}_b.png") img2 = pix.getImageData() display(Image(img2)) b_text = page.getText(clip=clip) 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) result.append((a_text, b_text)) df = pd.DataFrame(result, columns=["A", "B"]) df.to_excel("result.xlsx", index=False)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      前5頁(yè)運(yùn)行結(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

      1

      2

      3

      4

      5

      6

      7

      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)

      1

      2

      3

      4

      -------------- 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

      1

      2

      3

      4

      5

      6

      7

      Analysis Interval: 0 - 1000 [ms] Max(65.5 ms) = 8.15 g; Min(52.2 ms) = -7.426 g

      1

      2

      -------------- 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

      1

      2

      3

      4

      5

      6

      7

      Analysis Interval: 0 - 1000 [ms] Max(59.4 ms) = 71.87 g; Min(52 ms) = -9.89 g

      1

      2

      -------------- 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

      1

      2

      3

      4

      5

      6

      7

      Analysis Interval: 0 - 1000 [ms] Max(56.5 ms) = 20.39 g; Min(63.6 ms) = -23.43 g

      1

      2

      -------------- 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

      1

      2

      3

      4

      5

      6

      7

      Analysis Interval: 0 - 1000 [ms] Max(314.8 ms) = 0.2821 mm; Min(52.9 ms) = -33.24 mm

      1

      2

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:會(huì)計(jì)軟件:5種保持數(shù)據(jù)干凈的方法
      下一篇:wps怎么設(shè)置密碼(wps文檔怎么設(shè)置密碼)
      相關(guān)文章
      国产精品亚洲精品日韩动图| 亚洲国色天香视频| 亚洲人成网国产最新在线| 色拍自拍亚洲综合图区| 亚洲αv久久久噜噜噜噜噜| 中文字幕亚洲图片| 中文字幕无码精品亚洲资源网| 亚洲一级特黄大片无码毛片 | 亚洲色大成网站www永久男同| 精品亚洲成在人线AV无码| 亚洲国产精品乱码在线观看97 | 久久久久久亚洲av无码蜜芽| 亚洲大尺度无码无码专线一区| 亚洲精品无码少妇30P| 亚洲AV永久无码精品放毛片| 久久精品国产亚洲AV天海翼| 苍井空亚洲精品AA片在线播放| 国产精品亚洲专区无码牛牛| MM1313亚洲国产精品| 亚洲av无码国产精品色在线看不卡 | 亚洲AV无码乱码国产麻豆| 无码乱人伦一区二区亚洲一| 亚洲图片在线观看| 亚洲熟妇av一区| 欧洲 亚洲 国产图片综合| 亚洲精品理论电影在线观看| 亚洲AV噜噜一区二区三区| 亚洲第一永久AV网站久久精品男人的天堂AV| 亚洲国产精品尤物yw在线| 亚洲午夜国产精品无码老牛影视 | 亚洲综合中文字幕无线码| 亚洲熟妇av午夜无码不卡| 久久精品熟女亚洲av麻豆| 亚洲国产一区二区视频网站| 亚洲精品国偷自产在线| 亚洲精品天天影视综合网| 亚洲制服在线观看| 亚洲hairy多毛pics大全| 亚洲国产成人a精品不卡在线| 亚洲午夜福利在线观看| 亚洲系列中文字幕|