欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用PDFMiner.six解析PDF數(shù)據(jù)詳解

 更新時間:2025年03月10日 09:53:07   作者:夢想畫家  
PDFMiner.six 是基于 PDFMiner 項(xiàng)目開發(fā)的增強(qiáng)版,用于從PDF文檔中提取文本和結(jié)構(gòu)信息,下面我們就來學(xué)習(xí)一下如何使用PDFMiner.six解析PDF數(shù)據(jù)吧

PDF(可移植文檔格式)文件是由Adobe創(chuàng)建的一種靈活的文件格式,它允許文檔在不同的軟件、硬件和操作系統(tǒng)中一致地顯示。每個PDF文件都包含對固定布局文檔的全面描述,包括文本、字體、圖形和其他必要的顯示元素。pdf通常用于文檔共享,因?yàn)樗鼈兡軌虮3衷几袷?。然而,以編程方式解析和解釋PDF內(nèi)容可能是一項(xiàng)挑戰(zhàn)。這些困難包括pdf的復(fù)雜結(jié)構(gòu)、不同的文本編碼、復(fù)雜的布局、壓縮的內(nèi)容和嵌入的字體等問題。

我們最近評估了幾個流行的Python PDF庫,如PyPDF/PyPDF2, PDFMiner.six, PyMuPDF, PDFplumber2,等。有些庫適合提取文本,有些適合提取圖像,有些速度很快,等等。在本文中,我們將重點(diǎn)介紹如何開始使用PDFMiner.six。最新信息請隨時關(guān)注官方網(wǎng)站。

環(huán)境準(zhǔn)備

安裝依賴包:

pip install pdfminer.six
pip install 'pdfminer.six[image]'

示例PDF文件可以在這里找到,當(dāng)然你也可以自己準(zhǔn)備。讓我們看看如何使用這些api:

從PDF中提取文本

從PDF中提取圖像

迭代PDF中的所有對象

從PDF中提取TableOfContent (ToC)

抽取文本

通過高級API可用于從PDF中提取文本。

from pdfminer.high_level import extract_text
from os import path

path = path.abspath(path.dirname(__file__))
print(path)

pdf_file = path + '/sample01.pdf'
text = extract_text(pdf_file)
print(text)

抽取每一頁

from io import StringIO

from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.utils import open_filename

from os import path

path = path.abspath(path.dirname(__file__))
print(path)


def iter_text_per_page(pdf_file, password='', page_numbers=None, maxpages=0,
                 caching=True, codec='utf-8', laparams=None):
    if laparams is None:
        laparams = LAParams()

    with open_filename(pdf_file, "rb") as fp:
        rsrcmgr = PDFResourceManager(caching=caching)

        idx = 1
        for page in PDFPage.get_pages(
                fp,
                page_numbers,
                maxpages=maxpages,
                password=password,
                caching=caching,
        ):
            with StringIO() as output_string:
                device = TextConverter(rsrcmgr, output_string, codec=codec,
                                       laparams=laparams)
                interpreter = PDFPageInterpreter(rsrcmgr, device)
                interpreter.process_page(page)
                yield idx, output_string.getvalue()
                idx += 1


def main():
    pdf_file = path + '/sample02.pdf'
    for count, page_text in iter_text_per_page(pdf_file):
        print(f'page# {count}:\n{page_text}')
        print()


if __name__ == "__main__":
    main()

輸出內(nèi)容截取如下:

page# 1:

產(chǎn)品主要功能包括數(shù)據(jù)采集、數(shù)據(jù)治理以及數(shù)據(jù)產(chǎn)品應(yīng)用。企業(yè)典型應(yīng)用場景利用 AI 算法實(shí)現(xiàn)業(yè)務(wù)分類、聚類、回歸預(yù)測以及時間序列預(yù)測等。在銷售領(lǐng)域基于歷史數(shù)據(jù)實(shí)現(xiàn)銷售預(yù)測,基于用戶特征數(shù)據(jù)對客戶分類實(shí)現(xiàn)精準(zhǔn)營銷;在采購領(lǐng)域利用歷史數(shù)據(jù)預(yù)測采購價格,基于多維度指標(biāo)實(shí)現(xiàn)供應(yīng)商綜合評價模型等。

page# 2:
各類政策法規(guī)進(jìn)行整理和歸納,幫助用戶更加方便快捷地獲取所需的政策信息。。。。

抽取圖像

提取圖像的最簡單方法是調(diào)用命令行工具pdf2txt.py。它是在安裝PDFMiner時安裝的,并且位于Python可執(zhí)行文件的相同位置。使用的操作系統(tǒng)??蓤?zhí)行文件’查找Python二進(jìn)制文件的位置。

下面是示例用法:

usage: pdf2txt.py [-h] [--version] [--debug] [--disable-caching] [--page-numbers PAGE_NUMBERS [PAGE_NUMBERS ...]]
                  [--pagenos PAGENOS] [--maxpages MAXPAGES] [--password PASSWORD] [--rotation ROTATION] [--no-laparams]
                  [--detect-vertical] [--line-overlap LINE_OVERLAP] [--char-margin CHAR_MARGIN] [--word-margin WORD_MARGIN]
                  [--line-margin LINE_MARGIN] [--boxes-flow BOXES_FLOW] [--all-texts] [--outfile OUTFILE]
                  [--output_type OUTPUT_TYPE] [--codec CODEC] [--output-dir OUTPUT_DIR] [--layoutmode LAYOUTMODE]
                  [--scale SCALE] [--strip-control]
                  files [files ...]

To extract all text from pdf:
pdf2txt.py --all-texts ../samples/manual.pdf

To extract all images from pdf:
pdf2txt.py --output-dir images ../sample03.pdf

如果希望將其集成到應(yīng)用程序中,只需從pdf2txt.py復(fù)制源代碼即可.

獲取頁數(shù)

from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfparser import PDFParser
from pdfminer.pdftypes import resolve1

pdf_file = '../samples/brocher1.pdf'

with open(pdf_file, 'rb') as f:
    parser = PDFParser(f)
    doc = PDFDocument(parser)
    parser.set_document(doc)
    pages = resolve1(doc.catalog['Pages'])
    pages_count = pages.get('Count', 0)
    print(pages_count)

抽取表格數(shù)據(jù)

pdfminer抽取表格的輸出看起來比PyPDF2好得多,我們可以很容易地使用regex或split()提取所需的數(shù)據(jù)。但是在現(xiàn)實(shí)世界中,PDF文檔包含很多噪聲,id可以是不同的格式等等。我無法想象一個算法會考慮所有的事情。為了簡化和加快我們的工作,我建議將PDF文件轉(zhuǎn)換為HTML格式:

from io import StringIO
from pdfminer.high_level import extract_text_to_fp
from pdfminer.layout import LAParams

output = StringIO()
with open('example.pdf', 'rb') as pdf_file:
    extract_text_to_fp(pdf_file, output, laparams=LAParams(), output_type='html', codec=None)
with open('example.html', 'a') as html_file:
    html_file.write(output.getvalue())

然后再利用html標(biāo)簽處理庫抽取文本,這種方法準(zhǔn)確率應(yīng)該能得到保障。

到此這篇關(guān)于Python使用PDFMiner.six解析PDF數(shù)據(jù)詳解的文章就介紹到這了,更多相關(guān)Python解析PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論