python讀取pdf格式文檔的實現(xiàn)代碼
python讀取pdf文檔
一、 準(zhǔn)備工作
安裝對應(yīng)的庫 pip install pdfminer3k pip install pdfminer.six
二、部分變量的含義
PDFDocument(pdf文檔對象)
PDFPageInterpreter(解釋器)
PDFParser(pdf文檔分析器)
PDFResourceManager(資源管理器)
PDFPageAggregator(聚合器)
LAParams(參數(shù)分析器)
三、PDFMiner類之間的關(guān)系

PDFMiner的相關(guān)文檔(點擊跳轉(zhuǎn))
四、代碼實現(xiàn)
#!/usr/bin/env python # -*- coding:utf-8 -*- # datetime:2021/3/17 12:12 # software: PyCharm # version: python 3.9.2 def changePdfToText(filePath): """ 解析pdf 文本,保存到同名txt文件中 param: filePath: 需要讀取的pdf文檔的目錄 introduced module: from pdfminer.pdfpage import PDFPage from pdfminer.pdfparser import PDFParser from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.converter import PDFPageAggregator from pdfminer.layout import LAParams from pdfminer.pdfdocument import PDFDocument, PDFTextExtractionNotAllowed import os.path """ file = open(filePath, 'rb') # 以二進制讀模式打開 # 用文件對象來創(chuàng)建一個pdf文檔分析器 praser = PDFParser(file) # 創(chuàng)建一個PDF文檔 doc = PDFDocument(praser, '') # praser :上面創(chuàng)建的pdf文檔分析器 ,第二個參數(shù)是密碼,設(shè)置為空就好了 # 連接分析器 與文檔對象 praser.set_document(doc) # 檢測文檔是否提供txt轉(zhuǎn)換,不提供就忽略 if not doc.is_extractable: raise PDFTextExtractionNotAllowed # 創(chuàng)建PDf 資源管理器 來管理共享資源 rsrcmgr = PDFResourceManager() # 創(chuàng)建一個PDF設(shè)備對象 laparams = LAParams() device = PDFPageAggregator(rsrcmgr, laparams=laparams) # 創(chuàng)建一個PDF解釋器對象 interpreter = PDFPageInterpreter(rsrcmgr, device) result = [] # 內(nèi)容列表 # 循環(huán)遍歷列表,每次處理一個page的內(nèi)容 for page in PDFPage.create_pages(doc): interpreter.process_page(page) # 接受該頁面的LTPage對象 layout = device.get_result() for x in layout: if hasattr(x, "get_text"): result.append(x.get_text()) fileNames = os.path.splitext(filePath) # 分割 # 以追加的方式打開文件 with open(fileNames[0] + '.txt', 'a', encoding="utf-8") as f: results = x.get_text() # print(results) 這個句可以取消注釋就可以在控制臺將所有內(nèi)容輸出了 f.write(results) # 寫入文件 # 調(diào)用示例 : # path = u'E:\\1.pdf' # changePdfToText(path)
利用PyPDF2實現(xiàn)了對pdf文字內(nèi)容的提取
from PyPDF2 import PdfFileReader
# 定義獲取pdf內(nèi)容的方法
def getPdfContent(filename):
# 獲取PdfFileReader對象
pdf = PdfFileReader(open(filename, "rb"))
content = "" #content是輸出文本
for i in range(0,pdf.getNumPages()): #遍歷每一頁
pageObj = pdf.getPage(i)
try:
extractedText = pageObj.extractText()#導(dǎo)出每一頁的內(nèi)容,如果當(dāng)前頁有圖片的話就跳過
content += extractedText + "\n"
except BaseException:
pass
return content.encode("ascii", "ignore")
# 將獲取的內(nèi)容寫入txt文件
with open("test.txt","w") as f:
count=0 #count的作用是限制每一行的文字個數(shù),本人設(shè)置的是十行
#將獲取的文本變成字符串并用空白隔開
for item in str(getPdfContent("test.pdf")).split(" "):
# 如果當(dāng)前文字以句號結(jié)尾就換行
if item[-1]==".":
f.write(item+"\n")
count=0
else:
f.write(item+" ")
count +=1
# 如果寫了十個字就換行
if count==10:
f.write("\n")
# 重置count
count = 0
總結(jié)
到此這篇關(guān)于python讀取pdf格式文檔的文章就介紹到這了,更多相關(guān)python讀取pdf文檔內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Python中?Rembg庫實現(xiàn)去除圖片背景
這篇文章主要介紹了利用Python中?Rembg庫實現(xiàn)去除圖片背景,文章基于?Rembg庫得運用展開詳細(xì)介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05
python3.x上post發(fā)送json數(shù)據(jù)
這篇文章通過代碼示例給大家講述了python3.x上post發(fā)送json數(shù)據(jù)的詳細(xì)方法,一起學(xué)習(xí)下。2018-03-03
Pytorch 實現(xiàn)sobel算子的卷積操作詳解
今天小編就為大家分享一篇Pytorch 實現(xiàn)sobel算子的卷積操作詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

