一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

利用Python提取PDF文本得簡(jiǎn)單方法實(shí)例

目錄

你好,一般情況下,Ctrl+C 是最簡(jiǎn)單得方法,當(dāng)無(wú)法 Ctrl+C 時(shí),我們借助于 Python,以下是具體步驟:

第一步,安裝工具庫(kù)

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 venvsource venv/bin/activatepip install tika wand pytesseract

第二步,編寫代碼

假如 pdf 文件里面既有文字,又有圖片,以下代碼可以直接識(shí)別文字:

import ioimport pytesseractimport sys from PIL import Imagefrom tika import parserfrom 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 ioimport sys from PIL import Imageimport pytesseractfrom wand.image import Image as wifrom 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 是這樣得:

1f7cd9c778e1c3cfe5b9a0d36ea69272.png

在命令行這樣執(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ì)問(wèn),如果是簡(jiǎn)體中文,那個(gè) lang 參數(shù)傳遞什么,傳 'chi_sim',其實(shí)是有官方說(shuō)明得,鏈接如下:

https://github.com/tesseract-ocr/tessdoc/blob/main/Data-Files-in-different-versions.md

0361bb52f8ded0538c545369c2c0254f.png

最后得話

從 PDF 中提取文本得實(shí)現(xiàn)并不復(fù)雜,許多庫(kù)簡(jiǎn)化了工作并取得了很好得效果

到此這篇關(guān)于利用Python提取PDF文本得簡(jiǎn)單方法得內(nèi)容就介紹到這了,更多相關(guān)Python提取PDF文本內(nèi)容請(qǐng)搜索之家以前得內(nèi)容或繼續(xù)瀏覽下面得相關(guān)內(nèi)容希望大家以后多多支持之家!

聲明:所有內(nèi)容來(lái)自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評(píng)論
更多 網(wǎng)友評(píng)論1 條評(píng)論)
暫無(wú)評(píng)論

返回頂部

主站蜘蛛池模板: 激情国产 | 色综合色综合色综合 | 亚洲黄色网址视频 | 日韩精品一区二区三区视频播放 | 国产精品久久久久久久久久久久 | 欧美精品一二区 | 中文字幕91 | 理论片87福利理论电影 | 欧美黄色片 | 国产精品九九 | 毛片在线免费 | 在线观看视频一区 | 欧美激情亚洲 | 爱操av | 一区二区小视频 | 精品伦精品一区二区三区视频 | 一区不卡在线观看 | 免费在线观看一区二区三区 | 精品视频一区二区 | 天天操操操操操 | 中文字幕电影在线观看 | 九一视频在线播放 | 免费日韩网站 | 亚州毛片 | 九九国产 | 一级毛片色一级 | www.日韩 | 91在线精品秘密一区二区 | 国产黄色大片网站 | 日韩高清中文字幕 | 国产福利资源在线 | 中文字幕免费中文 | 一区二区在线观看免费视频 | av一区二区三区四区 | 成年人网站国产 | 久久精品1 | 91国产在线播放 | 国产精品第2页 | 亚州精品天堂中文字幕 | h片免费在线观看 | 国产目拍亚洲精品99久久精品 |