Python讀取pdf文件的簡(jiǎn)單代碼示例
安裝命令
需要安裝操作pdf的三方類(lèi)庫(kù),命令如下:
pip install pdfminer3K
安裝過(guò)程如下:
引入類(lèi)庫(kù)
需要引入很多的類(lèi)庫(kù)。
示例如下:
import sys import importlib importlib.reload(sys) 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 PDFTextExtractionNotAllowed
讀取pdf實(shí)現(xiàn)
實(shí)現(xiàn)步驟為:先通過(guò)二進(jìn)制方式打開(kāi)測(cè)試pdf文檔,創(chuàng)建pdf文檔解析測(cè)試文檔內(nèi)容,
最后讀取文件內(nèi)容,保存到另一個(gè)文件中。
示例如下:
import sys import importlib importlib.reload(sys) 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 PDFTextExtractionNotAllowed import os def read_pdf(path, toPath): # 以二進(jìn)制方式打開(kāi)pdf文件 f = open(path, 'rb') # 創(chuàng)建一個(gè)pdf文檔分析器 parser = PDFParser(f) # 創(chuàng)建pdf文檔 pdfFile = PDFDocument() # 鏈接分析器與文檔對(duì)象 parser.set_document(pdfFile) pdfFile.set_parser(parser) # 提供初始化密碼 pdfFile.initialize() # 檢測(cè)文檔是否提供txt轉(zhuǎn)換 if not pdfFile.is_extractable: raise PDFTextExtractionNotAllowed else: # 解析數(shù)據(jù) # 數(shù)據(jù)管理器 manager = PDFResourceManager() # 創(chuàng)建一個(gè)PDF設(shè)備對(duì)象 laparams = LAParams() device = PDFPageAggregator(manager, laparams=laparams) # 解釋器對(duì)象 interpreter = PDFPageInterpreter(manager, device) for page in pdfFile.get_pages(): interpreter.process_page(page) layout = device.get_result() for x in layout: if isinstance(x, LTTextBoxHorizontal): with open(toPath, 'a', encoding='utf-8') as f: print(x.get_text()) f.write(x.get_text() + "\n") path = os.path.join(os.getcwd(), 'test_1.pdf') toPath = os.path.join(os.getcwd(), 'test_2.txt') read_pdf(path, toPath)
注意:無(wú)法讀取中文,貌似需要加載中文字體。還有就是在寫(xiě)入pdf文件,格式不對(duì)無(wú)法打開(kāi)暫時(shí)沒(méi)找到原因。
附:python讀取PDF文件并做詞云可視化
import pdfplumber # 導(dǎo)入庫(kù) import jieba from wordcloud import WordCloud import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False # 用pdf文件解析器讀取文件 with pdfplumber.open('中華文化.pdf') as f: # 用for循環(huán)讀取文件中的每一頁(yè) for page in f.pages: text = page.extract_text() txt_f = open(r'中華文化.txt', mode='a', encoding='utf-8') # 創(chuàng)建txt文件 txt_f.write(text) # 寫(xiě)入txt文件 file = open('中華文化.txt',encoding='utf-8') file = file.read() #讀取txt文件 txtlist = jieba.lcut(file) string = " ".join(txtlist) stop_words = {} counts = {} for txt in txtlist: if len(txt) == 1: stop_words[txt] = stop_words.get(txt, 0) + 1 else: counts[txt] = counts.get(txt, 0) + 1 items = list(counts.items()) items.sort(key=lambda x: x[1], reverse=True) y1 = [] labels = [] for i in range(1,10): y1.append(items[i][1]) labels.append(items[i][0]) # plt.figure(figsize=(8,4)) width = 0.3 x = np.arange(len(y1)) a = [i for i in range(0,9)] plt.xticks(a,labels,rotation = 30) plt.bar(x=x,height=y1,width=width) plt.title('PDF文件中熱詞統(tǒng)計(jì)分析') plt.savefig("熱詞統(tǒng)計(jì)分析.png") plt.show() print("-------熱詞統(tǒng)計(jì)分析完成!-------") stoplist=[] item = list(stop_words.items()) for i in range(len(item)): txt,count = item[i] stoplist.append(txt) #print(stoplist) setlist = set(stoplist) wcd = WordCloud(width=1000, height=700, background_color='white', font_path='msyh.ttc', scale=15, stopwords=setlist) wcd.generate(string) wcd.to_image() print("-------熱詞詞云生成完成!-------") wcd.to_file('詞云.png') # 導(dǎo)出圖片
總結(jié)
本篇只是使用Python 實(shí)現(xiàn)讀取pdf文件簡(jiǎn)單示例,因?yàn)闀r(shí)間關(guān)系沒(méi)有做深入的擴(kuò)展,等之后有時(shí)間再做補(bǔ)充。
到此這篇關(guān)于Python讀取pdf文件的文章就介紹到這了,更多相關(guān)Python讀取pdf文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
celery在python爬蟲(chóng)中定時(shí)操作實(shí)例講解
在本篇文章里小編給大家整理了一篇關(guān)于celery在python爬蟲(chóng)中定時(shí)操作實(shí)例講解內(nèi)容,需要的朋友們可以參考下。2020-11-11Numpy中Meshgrid函數(shù)基本用法及2種應(yīng)用場(chǎng)景
NumPy包含很多實(shí)用的數(shù)學(xué)函數(shù),涵蓋線性代數(shù)運(yùn)算、傅里葉變換和隨機(jī)數(shù)生成等功能,下面這篇文章主要給大家介紹了關(guān)于Numpy中Meshgrid函數(shù)基本用法及2種應(yīng)用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2022-08-08Python+selenium實(shí)現(xiàn)截圖圖片并保存截取的圖片
這篇文章介紹如何利用Selenium的方法進(jìn)行截圖并保存截取的圖片,需要的朋友參考下本文2018-01-01Python發(fā)送郵件封裝實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Python發(fā)送郵件封裝實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05使用selenium+chromedriver+xpath爬取動(dòng)態(tài)加載信息
這篇文章主要介紹了使用selenium+chromedriver+xpath爬取動(dòng)態(tài)加載信息2022-02-02Python實(shí)現(xiàn)EM算法實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)EM算法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10python實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Python中使用pypdf2合并、分割、加密pdf文件的代碼詳解
這篇文章主要介紹了Python中使用pypdf2合并、分割、加密pdf文件的代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05