將pdf轉(zhuǎn)為txt或word的python小腳本

記錄一個(gè)提取pdf文字的小腳本

#!/usr/bin/python
# -*- coding: utf-8 -*-
 
import sys
import importlib

from pdfminer.pdfparser import PDFParser, PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import *
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed
 
'''
解析pdf文件,獲取文件中包含的各種對(duì)象
'''
# 解析pdf文件函數(shù)
def parse(pdf_path):
    fp = open(pdf_path, 'rb')  # 以二進(jìn)制讀模式打開(kāi)
    # 用文件對(duì)象來(lái)創(chuàng)建一個(gè)pdf文檔分析器
    parser = PDFParser(fp)
    # 創(chuàng)建一個(gè)PDF文檔
    doc = PDFDocument()
    # 連接分析器 與文檔對(duì)象
    parser.set_document(doc)
    doc.set_parser(parser)
 
    # 提供初始化密碼
    # 如果沒(méi)有密碼 就創(chuàng)建一個(gè)空的字符串
    doc.initialize()
 
    # 檢測(cè)文檔是否提供txt轉(zhuǎn)換,不提供就忽略
    if not doc.is_extractable:
        raise PDFTextExtractionNotAllowed
    else:
        # 創(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)
 
        # 用來(lái)計(jì)數(shù)頁(yè)面,圖片,曲線(xiàn),figure,水平文本框等對(duì)象的數(shù)量
        num_page, num_image, num_curve, num_figure, num_TextBoxHorizontal = 0, 0, 0, 0, 0
 
        # 循環(huán)遍歷列表,每次處理一個(gè)page的內(nèi)容
        for page in doc.get_pages():  # doc.get_pages() 獲取page列表
            num_page += 1  # 頁(yè)面增一
            interpreter.process_page(page)
            # 接受該頁(yè)面的LTPage對(duì)象
            layout = device.get_result()
            for x in layout:
                if isinstance(x, LTImage):  # 圖片對(duì)象
                    num_image += 1
                if isinstance(x, LTCurve):  # 曲線(xiàn)對(duì)象
                    num_curve += 1
                if isinstance(x, LTFigure):  # figure對(duì)象
                    num_figure += 1
                if isinstance(x, LTTextBoxHorizontal):  # 獲取文本內(nèi)容
                    num_TextBoxHorizontal += 1  # 水平文本框?qū)ο笤鲆?                    # 保存文本內(nèi)容
                    with open(r'生成的txt或doc路徑\\文件名.txt', 'a', encoding='utf-8') as f:  # 生成doc文件的文件名及路徑
                        results = x.get_text()
                        f.write(results)
                        f.write('\n')
        # print('對(duì)象數(shù)量:\n', '頁(yè)面數(shù):%s\n' % num_page, '圖片數(shù):%s\n' % num_image, '曲線(xiàn)數(shù):%s\n' % num_curve, '水平文本框:%s\n'
        #       % num_TextBoxHorizontal)

if __name__ == '__main__':
    pdf_path = r'pdf文件路徑\\文件名.pdf'
    parse(pdf_path)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容