Python实现PDF 转txt 和html转txt
Posted 会吃键盘的小懒熊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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个或者多个任意字符
此文章摘自多个网站的组合代码,以及好友支持,仅供借鉴
以上是关于Python实现PDF 转txt 和html转txt的主要内容,如果未能解决你的问题,请参考以下文章
5个实用的PDF自动化办公操作~1行Python代码搞定:解密加水印PPT/Word/TxT转PDF
word和.txt文件转html 及pdf文件, 使用poi jsoup itext心得