python 读取pdf转文字和提取目录

代码

import pyocr
import importlib
import sys
import time
from io import StringIO
importlib.reload(sys)
time1 = time.time()
# print("初始时间为:",time1)
import os.path
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument,PDFNoOutlines
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal,LAParams
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
from PyPDF2 import PdfFileReader as pdf_read
directory_str = ''
def bookmark_listhandler(list):
    global directory_str
    for message in list:
        if isinstance(message, dict):
            directory_str += message['/Title'] + '\n'
            # print(message['/Title'])
            #print(message)
        else:
            bookmark_listhandler(message)
# text_path = r'photo-words.pdf'
def file_name(file_dir):
    L=[]
    for i,j,files in os.walk(file_dir):
        L=files
        for file in files: 
            print(file)
    return L

def _parse_toc(doc):
    """With an open PDFDocument object, get the table of contents (toc) data
    [this is a higher-order function to be passed to with_pdf()]"""
    toc = []
    try:
        outlines = doc.get_outlines()
        for (level,title,dest,a,se) in outlines:
            print(level, title)
            toc.append((level, title))
    except PDFNoOutlines:
        pass
    #print(toc)
    return toc
def parse(pathtxt,text_path):
    '''解析PDF文本,并保存到TXT文件中'''
    print(text_path)
    fp = open(text_path,'rb')
    #用文件对象创建一个PDF文档分析器
    parser = PDFParser(fp)
    #创建一个PDF文档
    doc = PDFDocument(parser)
    #连接分析器,与文档对象
    parser.set_document(doc)
    #提供初始化密码,如果没有密码,就创建一个空的字符串
    #检测文档是否提供txt转换,不提供就忽略
    if not doc.is_extractable:
        raise PDFTextExtractionNotAllowed
        print("不提供")
    else:
        #创建PDF,资源管理器,来共享资源
        rsrcmgr = PDFResourceManager()
        #创建一个PDF设备对象
        laparams = LAParams()
        device = PDFPageAggregator(rsrcmgr,laparams=laparams)
        #创建一个PDF解释其对象
        interpreter = PDFPageInterpreter(rsrcmgr,device)
        #循环遍历列表,每次处理一个page内容
        # doc.get_pages() 获取page列表
        for page in PDFPage.create_pages(doc):
            interpreter.process_page(page)
            #接受该页面的LTPage对象
            layout = device.get_result()
            # 这里layout是一个LTPage对象 里面存放着 这个page解析出的各种对象
            # 一般包括LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等等
            # 想要获取文本就获得对象的text属性,
            for x in layout:
                if(isinstance(x,LTTextBoxHorizontal)):
                    with open(pathtxt,'a',encoding='utf-8') as f:
                        results = x.get_text()
                        #print(results)
                        f.write(results  +"\n")
if __name__ == '__main__':
    path='C:\\Users\\chenqi\\Desktop\\test\\QA-CivilAviationKG-master\\raw'
    files=file_name(path)
    i=1
    for file in files:
        names = file.split('.')
        print(names[0]+".pdf")
        pathtxt=names[0]+'text'+'.txt'
        print(pathtxt)
        parse(pathtxt,path+'\\'+file)
        i=i+1
    for i in range(len(files)):
        print(i, files[i])
        with open(path+'/'+files[i], 'rb') as f:
            pdf = pdf_read(f)
            # 检索文档中存在的文本大纲,返回的对象是一个嵌套的列表
            text_outline_list = pdf.getOutlines()
            bookmark_listhandler(text_outline_list)
        names = files[i].split('.')
        with open(names[0]+'title'+'.txt', 'w', encoding='utf-8') as f:
            f.write(directory_str)
    time2 = time.time()
    print("总共消耗时间为:",time2-time1)

效果

posted @ 2021-06-24 16:23  晨起  阅读(952)  评论(0编辑  收藏  举报