這篇文章將為大家詳細講解有關Python中pdfminer.six和pdfplumber模塊是什么,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創新互聯服務項目包括黎城網站建設、黎城網站制作、黎城網頁制作以及黎城網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,黎城網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到黎城省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
pdfminer.six
PDFMiner的操作門檻比較高,需要部分了解PDF的文檔結構模型,適合定制開發復雜的內容處理工具。
平時直接用PDFMiner比較少,這里只演示基本的文檔內容操作:
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-新冠肺炎疫情對中國連鎖餐飲行業的影響調研報告-中國連鎖經營協會.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使用門檻較高,但遇到復雜情況,最后還得用它。目前開源模塊中,它對PDF的支持應該是最全的了。
下面這個pdfplumber就是基于pdfminer.six開發的模塊,降低了使用門檻。
pdfplumber
相比pdfminer.six,pdfplumber提供了更便捷的PDF內容抽取接口。
日常工作中常用的操作,比如:
提取PDF內容,保存到txt文件
提取PDF中的表格到Excel
提取PDF中的圖片
提取PDF中的圖表
提取PDF內容,保存到txt文件
import pathlib import pdfplumber path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/002pdf') f_path = path.joinpath('2020-新冠肺炎疫情對中國連鎖餐飲行業的影響調研報告-中國連鎖經營協會.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-新冠肺炎疫情對中國連鎖餐飲行業的影響調研報告-中國連鎖經營協會.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的功能創建了一個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-疫情影響下的中國社區趨勢研究-艾瑞.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中的圖表
圖表與圖像不同,指的是類似直方圖、餅圖之類的數據生成圖。
import pathlib import pdfplumber from PIL import Image path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/002pdf') f_path = path.joinpath('2020-新冠肺炎疫情對中國連鎖餐飲行業的影響調研報告-中國連鎖經營協會.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標準規范由Adobe公司主導。
平時我們不需要參考規范,但如果遇到一些較復雜的場景,尤其是模塊沒有直接支持,就只能硬著頭皮翻閱文檔了。文檔是公開的,可以去搜索引擎搜索關鍵詞:pdf_reference_1-7.pdf。
關于Python中pdfminer.six和pdfplumber模塊是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享文章:Python中pdfminer.six和pdfplumber模塊是什么
文章鏈接:http://m.newbst.com/article4/pjdhie.html
成都網站建設公司_創新互聯,為您提供品牌網站制作、全網營銷推廣、軟件開發、網站設計、自適應網站、建站公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯