前言
大家好,我是空空star,本篇給大家分享一下
《通過Python的pdfplumber庫提取pdf中的文字》
。
之前給大家分享了《通過Python的PyPDF2庫提取pdf中的文字》
,感興趣的同學可以閱讀下。
本篇演示所用python版本:Python 3.8.9
通過Python的PyPDF2庫提取pdf中的文字
一、pdfplumber庫是什么?
pdfplumber是一個用于從PDF文檔中提取文本和表格數(shù)據(jù)的Python庫。它可以幫助用戶輕松地從PDF文件中提取有用的信息,例如表格、文本、元數(shù)據(jù)等。pdfplumber庫的特點包括:簡單易用、速度快、支持多種PDF文件格式、支持從多個頁面中提取數(shù)據(jù)等。pdfplumber庫還提供了一些方便的方法來處理提取的數(shù)據(jù),例如排序、過濾和格式化等。它是一個非常有用的工具,特別是在需要從大量PDF文件中提取數(shù)據(jù)時。
二、安裝pdfplumber庫
pip install pdfplumber
三、查看pdfplumber庫版本
pip show pdfplumber
Name: pdfplumber
Version: 0.9.0
Summary: Plumb a PDF for detailed information about each char, rectangle, and line.
Home-page: https://github.com/jsvine/pdfplumber
Author: Jeremy Singer-Vine
Author-email: jsvine@gmail.com
License:
Requires: pdfminer.six, Pillow, Wand
Required-by:文章來源:http://www.zghlxwxcb.cn/news/detail-462112.html
四、pdfplumber和PyPDF2區(qū)別是什么?
pdfplumber和PyPDF2都是Python中用于處理PDF文件的庫,但它們有一些區(qū)別:
功能:pdfplumber提供了更多的功能,例如提取表格、提取圖片、提取鏈接等,而PyPDF2只能提取文本和元數(shù)據(jù)。
速度:pdfplumber比PyPDF2更快,因為它使用了C語言的pdfminer庫進行解析。
API:pdfplumber的API更加直觀和易用,而PyPDF2的API有時候會讓人感到困惑。
總之,如果你需要提取PDF文件中的表格、圖片或鏈接等內(nèi)容,那么pdfplumber是一個更好的選擇。如果你只需要提取文本和元數(shù)據(jù),那么PyPDF2也可以勝任。文章來源地址http://www.zghlxwxcb.cn/news/detail-462112.html
五、使用方法
1.引入庫
import pdfplumber
2.定義pdf路徑
local = '/Users/kkstar/Downloads/'
3.打開PDF文件
with pdfplumber.open(local+"demo.pdf") as pdf:
4.獲取PDF文件中的頁數(shù)
num_pages = len(pdf.pages)
5.遍歷每一頁
for page_num in range(num_pages):
6.獲取當前頁內(nèi)容
page = pdf.pages[page_num]
7.提取文本內(nèi)容
text = page.extract_text()
8.打印文本內(nèi)容
print(text)
9.效果
大家好,我是空空star,這是第一頁。
大家好,我是空空star,這是第二頁。
大家好,我是空空star,這是第三頁。
Process finished with exit code 0
總結(jié)
到了這里,關(guān)于通過Python的pdfplumber庫提取pdf中的文字的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!