利用Python提取PDF文本的簡(jiǎn)單方法實(shí)例
你好,一般情況下,Ctrl+C 是最簡(jiǎn)單的方法,當(dāng)無法 Ctrl+C 時(shí),我們借助于 Python,以下是具體步驟:
第一步,安裝工具庫
1、tika — 用于從各種文件格式中進(jìn)行文檔類型檢測(cè)和內(nèi)容提取
2、wand — 基于 ctypes 的簡(jiǎn)單 ImageMagick 綁定
3、pytesseract — OCR 識(shí)別工具
創(chuàng)建一個(gè)虛擬環(huán)境,安裝這些工具
python -m venv venv source venv/bin/activate pip install tika wand pytesseract
第二步,編寫代碼
假如 pdf 文件里面既有文字,又有圖片,以下代碼可以直接識(shí)別文字:
import io import pytesseract import sys from PIL import Image from tika import parser from wand.image import Image as wi text_raw = parser.from_file("example.pdf") print(text_raw['content'].strip())
這還不夠,我們還需要能失敗圖片的部分:
def extract_text_image(from_file, lang='deu', image_type='jpeg', resolution=300): print("-- Parsing image", from_file, "--") print("---------------------------------") pdf_file = wi(filename=from_file, resolution=resolution) image = pdf_file.convert(image_type) image_blobs = [] for img in image.sequence: img_page = wi(image=img) image_blobs.append(img_page.make_blob(image_type)) extract = [] for img_blob in image_blobs: image = Image.open(io.BytesIO(img_blob)) text = pytesseract.image_to_string(image, lang=lang) extract.append(text) for item in extract: for line in item.split("\n"): print(line)
合并一下,完整代碼如下:
import io import sys from PIL import Image import pytesseract from wand.image import Image as wi from tika import parser def extract_text_image(from_file, lang='deu', image_type='jpeg', resolution=300): print("-- Parsing image", from_file, "--") print("---------------------------------") pdf_file = wi(filename=from_file, resolution=resolution) image = pdf_file.convert(image_type) for img in image.sequence: img_page = wi(image=img) image = Image.open(io.BytesIO(img_page.make_blob(image_type))) text = pytesseract.image_to_string(image, lang=lang) for part in text.split("\n"): print("{}".format(part)) def parse_text(from_file): print("-- Parsing text", from_file, "--") text_raw = parser.from_file(from_file) print("---------------------------------") print(text_raw['content'].strip()) print("---------------------------------") if __name__ == '__main__': parse_text(sys.argv[1]) extract_text_image(sys.argv[1], sys.argv[2])
第三步,執(zhí)行
假如 example.pdf 是這樣的:
在命令行這樣執(zhí)行:
python run.py example.pdf deu | xargs -0 echo > extract.txt
最終 extract.txt 的結(jié)果如下:
-- Parsing text example.pdf --
---------------------------------
Title pure text
Content pure text
Slide 1
Slide 2
---------------------------------
-- Parsing image example.pdf --
---------------------------------
Title pure text
Content pure text
Title in image
Text in image
你可能會(huì)問,如果是簡(jiǎn)體中文,那個(gè) lang 參數(shù)傳遞什么,傳 'chi_sim',其實(shí)是有官方說明的,鏈接如下:
https://github.com/tesseract-ocr/tessdoc/blob/main/Data-Files-in-different-versions.md
最后的話
從 PDF 中提取文本的腳本實(shí)現(xiàn)并不復(fù)雜,許多庫簡(jiǎn)化了工作并取得了很好的效果
到此這篇關(guān)于利用Python提取PDF文本的簡(jiǎn)單方法的文章就介紹到這了,更多相關(guān)Python提取PDF文本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pandas.DataFrame重置Series的索引index(reset_index)
本文主要介紹了Pandas.DataFrame重置Series的索引index(reset_index),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python 實(shí)現(xiàn)敏感目錄掃描的示例代碼
這篇文章主要介紹了Python 實(shí)現(xiàn)敏感目錄掃描的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05使用python實(shí)現(xiàn)簡(jiǎn)單五子棋游戲
這篇文章主要為大家詳細(xì)介紹了使用python實(shí)現(xiàn)簡(jiǎn)單五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06淺談PyQt5 的幫助文檔查找方法,可以查看每個(gè)類的方法
今天小編就為大家分享一篇淺談PyQt5 的幫助文檔查找方法,可以查看每個(gè)類的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python中的np.argmin()和np.argmax()函數(shù)用法
這篇文章主要介紹了Python中的np.argmin()和np.argmax()函數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06python利用高階函數(shù)實(shí)現(xiàn)剪枝函數(shù)
這篇文章主要為大家詳細(xì)介紹了python利用高階函數(shù)實(shí)現(xiàn)剪枝函數(shù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03用Python編寫一個(gè)簡(jiǎn)單的FUSE文件系統(tǒng)的教程
這篇文章主要介紹了用Python編寫一個(gè)簡(jiǎn)單的FUSE文件系統(tǒng)的教程,對(duì)于數(shù)據(jù)的備份很有幫助,需要的朋友可以參考下2015-04-04python3.7.3版本和django2.2.3版本是否可以兼容
在本篇文章里小編給大家整理的是一篇關(guān)于python3.7.3版本和django2.2.3版本是否可以兼容的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-09-09Python實(shí)現(xiàn)簡(jiǎn)單HTML表格解析的方法
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單HTML表格解析的方法,涉及Python基于libxml2dom模塊操作html頁面元素的技巧,需要的朋友可以參考下2015-06-06