Python使用PDFMiner.six解析PDF數(shù)據(jù)詳解
PDF(可移植文檔格式)文件是由Adobe創(chuàng)建的一種靈活的文件格式,它允許文檔在不同的軟件、硬件和操作系統(tǒng)中一致地顯示。每個PDF文件都包含對固定布局文檔的全面描述,包括文本、字體、圖形和其他必要的顯示元素。pdf通常用于文檔共享,因為它們能夠保持原始格式。然而,以編程方式解析和解釋PDF內容可能是一項挑戰(zhàn)。這些困難包括pdf的復雜結構、不同的文本編碼、復雜的布局、壓縮的內容和嵌入的字體等問題。
我們最近評估了幾個流行的Python PDF庫,如PyPDF/PyPDF2, PDFMiner.six, PyMuPDF, PDFplumber2,等。有些庫適合提取文本,有些適合提取圖像,有些速度很快,等等。在本文中,我們將重點介紹如何開始使用PDFMiner.six。最新信息請隨時關注官方網站。
環(huán)境準備
安裝依賴包:
pip install pdfminer.six pip install 'pdfminer.six[image]'
示例PDF文件可以在這里找到,當然你也可以自己準備。讓我們看看如何使用這些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()
輸出內容截取如下:
page# 1:
產品主要功能包括數(shù)據(jù)采集、數(shù)據(jù)治理以及數(shù)據(jù)產品應用。企業(yè)典型應用場景利用 AI 算法實現(xiàn)業(yè)務分類、聚類、回歸預測以及時間序列預測等。在銷售領域基于歷史數(shù)據(jù)實現(xiàn)銷售預測,基于用戶特征數(shù)據(jù)對客戶分類實現(xiàn)精準營銷;在采購領域利用歷史數(shù)據(jù)預測采購價格,基于多維度指標實現(xiàn)供應商綜合評價模型等。
page# 2:
各類政策法規(guī)進行整理和歸納,幫助用戶更加方便快捷地獲取所需的政策信息。。。。
抽取圖像
提取圖像的最簡單方法是調用命令行工具pdf2txt.py。它是在安裝PDFMiner時安裝的,并且位于Python可執(zhí)行文件的相同位置。使用的操作系統(tǒng)??蓤?zhí)行文件’查找Python二進制文件的位置。
下面是示例用法:
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
如果希望將其集成到應用程序中,只需從pdf2txt.py復制源代碼即可.
獲取頁數(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)實世界中,PDF文檔包含很多噪聲,id可以是不同的格式等等。我無法想象一個算法會考慮所有的事情。為了簡化和加快我們的工作,我建議將PDF文件轉換為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標簽處理庫抽取文本,這種方法準確率應該能得到保障。
到此這篇關于Python使用PDFMiner.six解析PDF數(shù)據(jù)詳解的文章就介紹到這了,更多相關Python解析PDF內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python中不可變數(shù)據(jù)類型原理與實戰(zhàn)全解析
在Python的世界里,數(shù)據(jù)對象被明確劃分為兩大陣營:可變(Mutable)與不可變(Immutable),而本文將為大家詳細介紹一下其中的不可變數(shù)據(jù),感興趣的可以了解下2025-04-04Python多維/嵌套字典數(shù)據(jù)無限遍歷的實現(xiàn)
下面小編就為大家?guī)硪黄狿ython多維/嵌套字典數(shù)據(jù)無限遍歷的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11python訪問類中docstring注釋的實現(xiàn)方法
這篇文章主要介紹了python訪問類中docstring注釋的實現(xiàn)方法,涉及python類注釋的訪問技巧,非常具有實用價值,需要的朋友可以參考下2015-05-05在Linux上安裝Python的Flask框架和創(chuàng)建第一個app實例的教程
這篇文章主要介紹了在Linux上安裝Python的Flask框架和創(chuàng)建第一個app實例,包括創(chuàng)建一個HTML模版和利用Jinja2模板引擎來做渲染的步驟,需要的朋友可以參考下2015-03-03