佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > python讀取pdf格式文檔的實(shí)現(xiàn)代碼

python讀取pdf格式文檔的實(shí)現(xiàn)代碼

熱門標(biāo)簽:呼叫中心市場(chǎng)需求 語(yǔ)音系統(tǒng) 百度AI接口 Win7旗艦版 硅谷的囚徒呼叫中心 客戶服務(wù) 企業(yè)做大做強(qiáng) 電話運(yùn)營(yíng)中心

python讀取pdf文檔

一、 準(zhǔn)備工作

安裝對(duì)應(yīng)的庫(kù)
	pip install pdfminer3k
	pip install pdfminer.six 

二、部分變量的含義

PDFDocument(pdf文檔對(duì)象)
PDFPageInterpreter(解釋器)
PDFParser(pdf文檔分析器)
PDFResourceManager(資源管理器)
PDFPageAggregator(聚合器)
LAParams(參數(shù)分析器)

三、PDFMiner類之間的關(guān)系

PDFMiner的相關(guān)文檔(點(diǎn)擊跳轉(zhuǎn))

四、代碼實(shí)現(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') # 以二進(jìn)制讀模式打開
 # 用文件對(duì)象來(lái)創(chuàng)建一個(gè)pdf文檔分析器
 praser = PDFParser(file)
 # 創(chuàng)建一個(gè)PDF文檔
 doc = PDFDocument(praser, '') # praser :上面創(chuàng)建的pdf文檔分析器 ,第二個(gè)參數(shù)是密碼,設(shè)置為空就好了
 # 連接分析器 與文檔對(duì)象
 praser.set_document(doc)
 # 檢測(cè)文檔是否提供txt轉(zhuǎn)換,不提供就忽略
 if not doc.is_extractable:
 raise PDFTextExtractionNotAllowed
 # 創(chuàng)建PDf 資源管理器 來(lái)管理共享資源
 rsrcmgr = PDFResourceManager()
 # 創(chuàng)建一個(gè)PDF設(shè)備對(duì)象
 laparams = LAParams()
 device = PDFPageAggregator(rsrcmgr, laparams=laparams)
 # 創(chuàng)建一個(gè)PDF解釋器對(duì)象
 interpreter = PDFPageInterpreter(rsrcmgr, device)
 result = [] # 內(nèi)容列表
 # 循環(huán)遍歷列表,每次處理一個(gè)page的內(nèi)容
 for page in PDFPage.create_pages(doc):
 interpreter.process_page(page)
 # 接受該頁(yè)面的LTPage對(duì)象
 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) 這個(gè)句可以取消注釋就可以在控制臺(tái)將所有內(nèi)容輸出了
   f.write(results) # 寫入文件

# 調(diào)用示例 :

# path = u'E:\\1.pdf'
# changePdfToText(path)

利用PyPDF2實(shí)現(xiàn)了對(duì)pdf文字內(nèi)容的提取

from PyPDF2 import PdfFileReader

# 定義獲取pdf內(nèi)容的方法
def getPdfContent(filename):
  # 獲取PdfFileReader對(duì)象
  pdf = PdfFileReader(open(filename, "rb"))
  content = "" #content是輸出文本
  for i in range(0,pdf.getNumPages()): #遍歷每一頁(yè)
    pageObj = pdf.getPage(i)
    try:
      extractedText = pageObj.extractText()#導(dǎo)出每一頁(yè)的內(nèi)容,如果當(dāng)前頁(yè)有圖片的話就跳過
      content += extractedText + "\n"
    except BaseException:
      pass
  return content.encode("ascii", "ignore")


# 將獲取的內(nèi)容寫入txt文件
with open("test.txt","w") as f:
  count=0 #count的作用是限制每一行的文字個(gè)數(shù),本人設(shè)置的是十行
  #將獲取的文本變成字符串并用空白隔開
  for item in str(getPdfContent("test.pdf")).split(" "):
    # 如果當(dāng)前文字以句號(hào)結(jié)尾就換行
    if item[-1]==".":
      f.write(item+"\n")
      count=0
    else:
      f.write(item+" ")
      count +=1
    # 如果寫了十個(gè)字就換行
    if count==10:
      f.write("\n")
      # 重置count
      count = 0

總結(jié)

到此這篇關(guān)于python讀取pdf格式文檔的文章就介紹到這了,更多相關(guān)python讀取pdf文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python解析并讀取PDF文件內(nèi)容的方法
  • Python2.7讀取PDF文件的方法示例
  • python 使用pdfminer3k 讀取PDF文檔的例子
  • Python讀取pdf表格寫入excel的方法

標(biāo)簽:山西 濟(jì)南 喀什 長(zhǎng)沙 崇左 山西 海南 安康

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python讀取pdf格式文檔的實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    牡丹江市| 宁波市| 三门峡市| 深泽县| 绥棱县| 军事| 合山市| 新乡市| 长乐市| 斗六市| 临沭县| 喀喇沁旗| 日喀则市| 泸州市| 临西县| 包头市| 灵山县| 万山特区| 上蔡县| 九龙县| 福海县| 任丘市| 托里县| 子长县| 江口县| 确山县| 华安县| 苗栗县| 周至县| 乐安县| 星座| 驻马店市| 高雄县| 许昌市| 岳普湖县| 新郑市| 鄂伦春自治旗| 黔南| 达日县| 墨脱县| 甘泉县|