日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

io python 读取pdf_Python读取PDF文件--pdfminer

發(fā)布時(shí)間:2023/12/4 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 io python 读取pdf_Python读取PDF文件--pdfminer 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

作者使用的是Python3.6版本。

pdfminer在Python2和Python3中的安裝和使用有一定的區(qū)別,本文以Python為例。

首先安裝pdfminer

pip install pdfminer3k

官網(wǎng)對(duì)PDFMiner的介紹如下:

PDFMiner is a tool for extracting information from PDF documents. Unlike other PDF-related tools, it focuses entirely on getting and analyzing text data. PDFMiner allows to obtain the exact location of texts in a page, as well as other information such as fonts or lines. It includes a PDF converter that can transform PDF files into other text formats (such as HTML). It has an extensible PDF parser that can be used for other purposes instead of text analysis.

PDF的格式不是規(guī)范的,很多情況下沒有邏輯結(jié)構(gòu),不能自適應(yīng)頁面大小的調(diào)整。PDFMiner是通過嘗試猜測(cè)PDF的布局來重建其結(jié)構(gòu),有時(shí)候效果并不理想。

import importlib import sys import timeimportlib.reload(sys) time1 = time.time()import os.path from pdfminer.pdfparser import PDFParser,PDFDocument from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.converter import PDFPageAggregator from pdfminer.layout import LTTextBoxHorizontal,LAParams from pdfminer.pdfinterp import PDFTextExtractionNotAlloweddef parse(pdf_path,txt_path):'''解析PDF文本,并保存到TXT文件中'''fp = open(text_path,'rb')# pdf1 = urlopen('http://www.tencent.com/20160321.pdf')#用文件對(duì)象創(chuàng)建一個(gè)PDF文檔分析器parser = PDFParser(fp)#創(chuàng)建一個(gè)PDF文檔doc = PDFDocument()#連接分析器,與文檔對(duì)象parser.set_document(doc)doc.set_parser(parser)#提供初始化密碼,如果沒有密碼,就創(chuàng)建一個(gè)空的字符串doc.initialize()#檢測(cè)文檔是否提供txt轉(zhuǎn)換,不提供就忽略if not doc.is_extractable:raise PDFTextExtractionNotAllowedelse:#創(chuàng)建PDF,資源管理器,來共享資源rsrcmgr = PDFResourceManager()#創(chuàng)建一個(gè)PDF設(shè)備對(duì)象laparams = LAParams()device = PDFPageAggregator(rsrcmgr,laparams=laparams)#創(chuàng)建一個(gè)PDF解釋其對(duì)象interpreter = PDFPageInterpreter(rsrcmgr,device)#循環(huán)遍歷列表,每次處理一個(gè)page內(nèi)容# doc.get_pages() 獲取page列表for page in doc.get_pages():interpreter.process_page(page)#接受該頁面的LTPage對(duì)象layout = device.get_result()# 這里layout是一個(gè)LTPage對(duì)象 里面存放著 這個(gè)page解析出的各種對(duì)象# 一般包括LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等等# 想要獲取文本就獲得對(duì)象的text屬性,for x in layout:if(isinstance(x,LTTextBoxHorizontal)):with open(txt_path,'a') as f:results = x.get_text()print(results)f.write(results +"n")if __name__ == '__main__':pdf_path = './test.pdf'txt_path = './test.txt'parse(pdf_path,txt_path)time2 = time.time()print("總共消耗時(shí)間為:",time2-time1)

上述代碼只能解析正常的PDF內(nèi)容,在實(shí)際應(yīng)用場(chǎng)景中,很多PDF文檔可能是加密的,如何處理解密的PDF文件,也是我們需要get的技能。

pydf2這個(gè)庫能夠?qū)崿F(xiàn)對(duì)PDF文件進(jìn)行加密和解密。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的io python 读取pdf_Python读取PDF文件--pdfminer的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。