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

  • 相关阅读:
    爬虫开发9.scrapy框架之递归解析和post请求
    爬虫开发7.scrapy框架简介和基础应用
    爬虫开发6.selenuim和phantonJs处理网页动态加载数据的爬取
    爬虫开发4.三种数据解析方式
    Gym–101061A Cards(有待更新)
    GYM 101061 I. Playing with strings(有待更新)
    HDU2072 单词数
    HDU2057 A + B Again(十六进制加法运算)
    HDU2056 Rectangles
    CodeForces 992C Nastya and a Wardrobe
  • 原文地址:https://www.cnblogs.com/sunmoon1993/p/10334196.html
Copyright © 2011-2022 走看看