日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python中pdfminer.six和pdfplumber模塊是什么-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)Python中pdfminer.six和pdfplumber模塊是什么,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)主營赤峰林西網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,赤峰林西h5微信小程序定制開發(fā)搭建,赤峰林西網(wǎng)站營銷推廣歡迎赤峰林西等地區(qū)企業(yè)咨詢

pdfminer.six

PDFMiner的操作門檻比較高,需要部分了解PDF的文檔結(jié)構(gòu)模型,適合定制開發(fā)復(fù)雜的內(nèi)容處理工具。

平時直接用PDFMiner比較少,這里只演示基本的文檔內(nèi)容操作:

import pathlib from pdfminer.pdfparser import PDFParser from pdfminer.pdfdocument import PDFDocument from pdfminer.pdfpage import PDFPage from pdfminer.pdfinterp import PDFResourceManager from pdfminer.pdfinterp import PDFPageInterpreter from pdfminer.pdfdevice import PDFDevice from pdfminer.layout import LAParams, LTTextBox, LTFigure, LTImage from pdfminer.converter import PDFPageAggregator path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/002pdf') f_path = path.joinpath('2020-新冠肺炎疫情對中國連鎖餐飲行業(yè)的影響調(diào)研報告-中國連鎖經(jīng)營協(xié)會.pdf') with open(f_path, 'rb') as f:    parser = PDFParser(f)    doc = PDFDocument(parser)    rsrcmgr = PDFResourceManager()    laparams = LAParams()    device = PDFPageAggregator(rsrcmgr, laparams=laparams)    interpreter = PDFPageInterpreter(rsrcmgr, device)    for page in PDFPage.create_pages(doc):        interpreter.process_page(page)        layout = device.get_result()        for x in layout:            # 獲取文本對象            if isinstance(x, LTTextBox):                print(x.get_text().strip())            # 獲取圖片對象            if isinstance(x,LTImage):                print('這里獲取到一張圖片')            # 獲取 figure 對象            if isinstance(x,LTFigure):                print('這里獲取到一個 figure 對象')

雖然pdfminer使用門檻較高,但遇到復(fù)雜情況,最后還得用它。目前開源模塊中,它對PDF的支持應(yīng)該是最全的了。

下面這個pdfplumber就是基于pdfminer.six開發(fā)的模塊,降低了使用門檻。

pdfplumber

相比pdfminer.six,pdfplumber提供了更便捷的PDF內(nèi)容抽取接口。

日常工作中常用的操作,比如:

  • 提取PDF內(nèi)容,保存到txt文件

  • 提取PDF中的表格到Excel

  • 提取PDF中的圖片

  • 提取PDF中的圖表

提取PDF內(nèi)容,保存到txt文件

import pathlib import pdfplumber path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/002pdf') f_path = path.joinpath('2020-新冠肺炎疫情對中國連鎖餐飲行業(yè)的影響調(diào)研報告-中國連鎖經(jīng)營協(xié)會.pdf') out_path = path.joinpath('002pdf_out.txt') with pdfplumber.open(f_path) as pdf, open(out_path ,'a') as txt:    for page in pdf.pages:        textdata = page.extract_text()        txt.write(textdata)

提取PDF中的表格到Excel

import pathlib import pdfplumber from openpyxl import Workbook path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/002pdf') f_path = path.joinpath('2020-新冠肺炎疫情對中國連鎖餐飲行業(yè)的影響調(diào)研報告-中國連鎖經(jīng)營協(xié)會.pdf') out_path = path.joinpath('002pdf_excel.xlsx') wb = Workbook() sheet = wb.active with pdfplumber.open(f_path) as pdf:    for i in range(19, 22):        page = pdf.pages[i]        table = page.extract_table()        for row in table:            sheet.append(row) wb.save(out_path)

上面用到了openpyxl的功能創(chuàng)建了一個Excel文件,之前有單獨文章介紹它。

提取PDF中的圖片

import pathlib import pdfplumber from PIL import Image path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/002pdf') f_path = path.joinpath('2020-疫情影響下的中國社區(qū)趨勢研究-艾瑞.pdf') out_path = path.joinpath('002pdf_images.png') with pdfplumber.open(f_path) as pdf, open(out_path, 'wb') as fout:    page = pdf.pages[10]    # for img in page.images:    im = page.to_image()    im.save(out_path, format='PNG')    imgs = page.images    for i, img in enumerate(imgs):        size = img['width'], img['height']        data = img['stream'].get_data()        out_path = path.joinpath(f'002pdf_images_{i}.png')        with open(out_path, 'wb') as fimg_out:            fimg_out.write(data)

上面用到了PIL(Pillow)的功能處理圖片。

提取PDF中的圖表

圖表與圖像不同,指的是類似直方圖、餅圖之類的數(shù)據(jù)生成圖。

import pathlib import pdfplumber from PIL import Image path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/002pdf') f_path = path.joinpath('2020-新冠肺炎疫情對中國連鎖餐飲行業(yè)的影響調(diào)研報告-中國連鎖經(jīng)營協(xié)會.pdf') out_path = path.joinpath('002pdf_figures.png') with pdfplumber.open(f_path) as pdf, open(out_path, 'wb') as fout:    page = pdf.pages[7]    im = page.to_image()    im.save(out_path, format='PNG')    figures = page.figures    for i, fig in enumerate(figures):        size = fig['width'], fig['height']        crop = page.crop((fig['x0'], fig['top'], fig['x1'], fig['bottom']))        img_crop = crop.to_image()        out_path = path.joinpath(f'002pdf_figures_{i}.png')        img_crop.save(out_path, format='png')    im.draw_rects(page.extract_words(), stroke='yellow')    im.draw_rects(page.images, stroke='blue')    im.draw_rects(page.figures) im # show in notebook

另外需要說明的是,PDF標(biāo)準(zhǔn)規(guī)范由Adobe公司主導(dǎo)。

平時我們不需要參考規(guī)范,但如果遇到一些較復(fù)雜的場景,尤其是模塊沒有直接支持,就只能硬著頭皮翻閱文檔了。文檔是公開的,可以去搜索引擎搜索關(guān)鍵詞:pdf_reference_1-7.pdf。

關(guān)于Python中pdfminer.six和pdfplumber模塊是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。


本文名稱:Python中pdfminer.six和pdfplumber模塊是什么-創(chuàng)新互聯(lián)
文章URL:http://www.dlmjj.cn/article/cdohod.html