zoukankan      html  css  js  c++  java
  • Python实现PDF 转txt 和html转txt

    # -*- coding: utf-8 -*-
    from HTMLParser import HTMLParser
    from re import sub
    from sys import stderr
    from traceback import print_exc
    from pdfminer.pdfparser import PDFParser
    from pdfminer.pdfdocument import PDFDocument
    from pdfminer.pdfpage import PDFPage
    from pdfminer.pdfdevice import PDFDevice
    from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
    from pdfminer.converter import PDFPageAggregator
    from pdfminer.layout import LTTextBoxHorizontal,LAParams
    from pdfminer.pdfpage import PDFTextExtractionNotAllowed
    import re


    class pythonNToTxt(HTMLParser):
    def __init__(self):
    HTMLParser.__init__(self)
    self.__text = []

    def handle_data(self, data):
    text = data.strip()
    if len(text) > 0:
    text = sub('[ ]+', ' ', text)
    self.__text.append(text + ' ')

    def handle_starttag(self, tag, attrs):
    if tag == 'p':
    self.__text.append(' ')
    elif tag == 'br':
    self.__text.append(' ')

    def handle_startendtag(self, tag, attrs):
    if tag == 'br':
    self.__text.append(' ')

    def text(self):
    return ''.join(self.__text).strip()


    def dehtml(text):
    try:
    parser = pythonNToTxt()
    parser.feed(text)
    parser.close()
    return parser.text()
    except:
    print_exc(file=stderr)
    return text

    def html_to_txt(fileobject,saveName):
    text = r'''
    <html>
    <body>
    <b>Project:</b> DeHTML<br>
    <b>Description</b>:<br>
    <p>由HTML转换成txt文件.从HTML文件读取,存入test3.txt</p>
    </body>
    </html>
    '''
    neirong1 = open(fileobject, 'rb')
    neirong = neirong1.read()
    print neirong
    # for line in neirong:
    # #Path = open('af58a19ce7b54986a7515f330a48cde3.pdf', 'rb')
    # print(dehtml(line))
    # with open('%s' % (saveName), 'a') as f:
    # # results = dehtml(text).encode('utf-8')
    # results = dehtml(text)
    # f.write(results + " ")
    with open('%s' % (saveName), 'a') as f:
    results = dehtml(neirong)
    f.write(results + " ")


    def pdf_to_txt(filename, Save_name):
    fileobject = open(filename, 'rb')
    parser = PDFParser(fileobject)
    document = PDFDocument(parser)

    if not document.is_extractable:
    raise PDFTextExtractionNotAllowed
    else:
    rsrcmgr = PDFResourceManager()
    laparams = LAParams()
    device = PDFPageAggregator(rsrcmgr, laparams=laparams)
    interpreter = PDFPageInterpreter(rsrcmgr, device)

    for page in PDFPage.create_pages(document):
    interpreter.process_page(page)
    layout = device.get_result()
    for x in layout:
    if(isinstance(x, LTTextBoxHorizontal)):
    with open('%s' % (Save_name), 'a') as f:
    results = x.get_text().encode('utf-8')
    f.write(results + " ")

    # 分支判断
    filename = 'test3.html'

    fenzhihouzhui = re.findall(r'.*(..*)', str(filename))[0]
    if fenzhihouzhui == '.pdf' or fenzhihouzhui == '.PDF':
    pdf_to_txt(filename, '3.txt')
    elif fenzhihouzhui == '.html' or fenzhihouzhui == '.HTML':
    html_to_txt(filename, 'wenben1.txt')


    正则说明:

    第一个.*

    代表任意字符出现0次或者多次

    括号用来分组

    正则匹配到的值就来自于这个大括号里面

    .代表对.这个符号不做转义

    就是.的意思

    要不然正常情况.代表任意字符

    最后一个.*就是0个或者多个任意字符

    此文章摘自多个网站的组合代码,以及好友支持,仅供借鉴

    摘自:

    https://blog.csdn.net/quicktest/article/details/7852336

    https://www.cnblogs.com/wj-1314/p/9429816.html

  • 相关阅读:
    ubuntu 14.04+apache 反向代理设置
    ubuntu 14.04 使用apt-get出现如下问题解决办法
    ubuntu 出现 Unable to locate package update 解决办法
    在ubuntu 12.04 apache 限制IP访问的方法
    (原创)在ubuntu 14.04 中安装Apache2+modsecurity+awstats (新手教程)
    windows 10 的安装说明
    前端三大主流框架中文文档
    关于移动端影像配置了https之后拍出来的照片在android手机无法显示的问题
    ES5常用api
    promise循环调用异步函数(以图片上传为例)
  • 原文地址:https://www.cnblogs.com/sunmoon1993/p/10334196.html
Copyright © 2011-2022 走看看