網(wǎng)站首頁 編程語言 正文
你好,一般情況下,Ctrl+C 是最簡單的方法,當(dāng)無法 Ctrl+C 時,我們借助于 Python,以下是具體步驟:
第一步,安裝工具庫
1、tika — 用于從各種文件格式中進(jìn)行文檔類型檢測和內(nèi)容提取
2、wand — 基于 ctypes 的簡單 ImageMagick 綁定
3、pytesseract — OCR 識別工具
創(chuàng)建一個虛擬環(huán)境,安裝這些工具
python -m venv venv
source venv/bin/activate
pip install tika wand pytesseract
第二步,編寫代碼
假如 pdf 文件里面既有文字,又有圖片,以下代碼可以直接識別文字:
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
你可能會問,如果是簡體中文,那個 lang 參數(shù)傳遞什么,傳 'chi_sim',其實是有官方說明的,鏈接如下:
https://github.com/tesseract-ocr/tessdoc/blob/main/Data-Files-in-different-versions.md
最后的話
從 PDF 中提取文本的腳本實現(xiàn)并不復(fù)雜,許多庫簡化了工作并取得了很好的效果
原文鏈接:https://blog.csdn.net/somenzz/article/details/124440977
相關(guān)推薦
- 2022-10-29 tensorflow2數(shù)據(jù)讀取P2: tf.data.Dataset.from_generator通
- 2022-05-09 sql語句中union的用法與踩坑記錄_MsSql
- 2022-06-07 Docker的四種網(wǎng)絡(luò)模式_docker
- 2022-11-04 詳解C++?指針與二維數(shù)組名_C 語言
- 2022-02-17 ERROR: but there is no HDFS_NAMENODE_USER defined.
- 2022-06-01 Python學(xué)習(xí)之內(nèi)置函數(shù)總結(jié)_python
- 2022-06-12 SQLServer只賦予創(chuàng)建表權(quán)限的全過程_MsSql
- 2022-05-06 Spring Boot 使用 RequiredArgsConstructor 參數(shù) onConstr
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支