PDF-TXT-XML

第一步,从PDF转成TXT

注意:去除空格、空行等

__author__ = 'wangfei'
# -*- coding: utf-8 -*-
import sys
import os
reload(sys)
sys.setdefaultencoding('utf-8')
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import *
#手动输入,以后改成在某个文件夹下读取子文件
fp = open('pdf/sln.pdf', 'rb')
#用文件对象来创建一个pdf文档分析器
parser = PDFParser(fp)
# 创建一个  PDF 文档
doc = PDFDocument(parser)
# 检测文档是否提供txt转换,不提供就忽略
if not doc.is_extractable:
    raise PDFTextExtractionNotAllowed
# 创建PDf 资源管理器 来管理共享资源
rsrcmgr = PDFResourceManager()
# 创建一个PDF设备对象
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

# 处理文档对象中每一页的内容
# doc.get_pages() 获取page列表
# 循环遍历列表,每次处理一个page的内容
# 这里layout是一个LTPage对象 里面存放着 这个page解析出的各种对象
#  一般包括LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等等 想要获取文本就获得对象的text属性,
all = []
for page in PDFPage.create_pages(doc):
    interpreter.process_page(page)
    layout = device.get_result()
    for x in layout:
        if(isinstance(x, LTTextBox)):
			#coment by dz :delete the \n
            string = x.get_text().replace('\n', '')
            #print string
            all.append(string.strip())

#写入文件到txt目录
ls = os.linesep
fObj = open('txt/sln.txt', 'wb')
fObj.writelines(['%s%s' % (x, ls) for x in all])
fObj.close()


第二步,从TXT到XML

首先XML处理类:

__author__ = 'wangfei'

import xml.dom.minidom as Dom


class XMLGenerator:
    def __init__(self, xml_name):
        self.doc = Dom.Document()
        self.xml_name = xml_name

    def createNode(self, node_name):
        return self.doc.createElement(node_name)

    def addNode(self, node, pre_node = None):
        cur_node = node
        if pre_node is not None:
            pre_node.appendChild(cur_node)
        else:
            self.doc.appendChild(cur_node)
        return cur_node

    def setNodeAttr(self, node, att_name, value):
        cur_node = node
        cur_node.setAttribute(att_name, value)

    def setNodeValue(self, cur_node, value):
        node_data = self.doc.createTextNode(value)
        cur_node.appendChild(node_data)

    def genXML(self):
        f = open(self.xml_name, "w")
        f.write(self.doc.toprettyxml(indent="\t", newl="\n", encoding="utf-8"))
        f.close()

根据文本信息处理TXT文档,文本信息包括章节标题标记、图像、公式等

#! /usr/bin/env python
#coding:utf-8
import sys
import linecache
import re
from XMLGenerator import *

reload(sys)
sys.setdefaultencoding('utf-8')


fileName = "txt/sln.txt"
try:
    fobj = open(fileName, 'r')
except IOError, e:
    print("*** file open error:", e)
else:
	tittle = linecache.getline(fileName, 1).lower().strip('\n')
	xmlFile = XMLGenerator(tittle.strip('\n') + ".xml")
	#xml root node article
	#add article
	node_article = xmlFile.createNode("div")
	xmlFile.setNodeAttr(node_article,"id","article")
	xmlFile.addNode(node=node_article)
	
	#add tittle
	node_tittle =xmlFile.createNode("div")
	xmlFile.setNodeAttr(node_tittle,"id","tittle")
	xmlFile.setNodeAttr(node_tittle,"class","ltx_title ltx_title_document")
	xmlFile.addNode(node_tittle,node_article)
    
	#add intru
	node_intru = xmlFile.createNode("div")
	xmlFile.setNodeAttr(node_intru,"id","intru")
	xmlFile.setNodeAttr(node_intru,"class","ltx_p")
	xmlFile.addNode(node_intru,node_tittle)
	
	#NULL
	node_section= xmlFile.createNode("div")
	node_sub = xmlFile.createNode("div")
	
	n_sec = [([-1] * 15) for i in range(22)]
	section = []
	k=0
	sec=0
	sub=0
	sec_info=0
	sub_info_1=0
	sub_info_2=0
    
	#read paper section name
	for (num,eachLine) in enumerate(fobj):
		if num ==0:
			xmlFile.setNodeValue(node_tittle,eachLine.strip())
		elif num ==1:
			xmlFile.setNodeValue(node_intru,eachLine.strip())
		elif(eachLine != "\n"):
			p_set = '^2\.(\d+)'
			p_sub ='^2\.(\d+)\.(\d+)'
			p_num ='^\d\d+'
			p_fig ='^Fig'
			p_ch  = '^Chapter'
			p_h   ='^H\.'
			
			words = len(eachLine.split(' '))
			m_h   =re.search(p_h,eachLine)
			m_ch  = re.search(p_ch,eachLine)
			m_fig = re.search(p_fig,eachLine)
			m_num =re.search(p_num,eachLine)
			m_set = re.search(p_set,eachLine)
			m_sub = re.search(p_sub,eachLine)
			if(m_h == None and m_num==None and m_fig==None and m_ch==None and words>=9 or m_set!=None):
				k =k+1
				section.append(eachLine.strip())
				if(m_set!=None and m_sub==None):  #for the 2nd section
					sec_info = int(m_set.group(1))
					n_sec[sec_info][0]=k
					sec = k
					#add the next section to the cur node
					node_section = xmlFile.createNode("div")
					xmlFile.setNodeAttr(node_section,"id","s2.ss" + m_set.group(1))
					xmlFile.setNodeAttr(node_section, "class", "ltx_section")
					xmlFile.addNode(node_section, node_article)
					#xmlFile.setNodeValue(node_section,eachLine.strip())
					
					node_st =xmlFile.createNode("h2")
					xmlFile.setNodeAttr(node_st,"class","ltx_title ltx_titleh_section")
					xmlFile.addNode(node_st,node_section)
					xmlFile.setNodeValue(node_st,eachLine.strip())
					
					
				elif m_set!=None and m_sub!=None:
					sub_info_1 = int(m_sub.group(1))
					sub_info_2 = int(m_sub.group(2))
					print sub_info_1
					print sub_info_2
					n_sec[sub_info_1][sub_info_2]=k
					sub =k
					#add sub to section
					node_sub =xmlFile.createNode("div")
					xmlFile.setNodeAttr(node_sub,"id","s"+"2.ss"+m_sub.group(1)+".sss"+m_sub.group(2))
					xmlFile.setNodeAttr(node_sub,"class","ltx_subsection")
					xmlFile.addNode(node_sub,node_section)
					
					node_st =xmlFile.createNode("h2")
					xmlFile.setNodeAttr(node_st,"class","ltx_title ltx_titleh_subsection")
					xmlFile.addNode(node_st,node_sub)
					xmlFile.setNodeValue(node_st,eachLine.strip())
				else :
					#add paragraph
					if sec>sub: 
						n_para = str(k-sec) 
						node_para = xmlFile.createNode("div")
						xmlFile.setNodeAttr(node_para,"id","s2.ss"+str(sec_info)+".p"+n_para)
						xmlFile.setNodeAttr(node_sub,"class","ltx_para")
						xmlFile.addNode(node_para,node_section)
						xmlFile.setNodeValue(node_para,eachLine.strip())
					else :
						n_para = str(k-sub)
						node_para =xmlFile.createNode("div")
						xmlFile.setNodeAttr(node_para,"id","s2."+str(sub_info_1)+"."+str(sub_info_1)+".p"+n_para)
						xmlFile.setNodeAttr(node_para,"class","ltx_para")
						xmlFile.addNode(node_para,node_sub)
						xmlFile.setNodeValue(node_para,eachLine.strip())
	#gen
	xmlFile.genXML()
	fobj.close()



第一部分 XML简介 第1 章 XML概览 1.1 什么是XML 1.1.1 XML是元标记语言 1.1.2 XML描述的是结构和语义,而不是格式 1.2 为什么开发人员对XML感到激动 1.2.1 设计与特定领域有关的标记语言 1.2.2 自描述数据 1.2.3 应用间交换数据 1.2.4 结构化和集成的数据 1.3 XML文档的“生命” 1.3.1 编辑器 1.3.2 语法分析程序和处理程序 1.3.3 浏览器和其他工具 1.3.4 处理过程总结 .4 相关技术 1.4.1 超文本标记语言(Hypertext Markup Lan 1.4.2 级联样式单(Cascading Style Sheets) 1.4.3 可扩展的样式语言(Extensible Style Lan 1.4.4 URL和URI 1.4.5 XLink和XPointer 1.4.6 Unicode字符集 1.4.7 如何将这些技术融合在一起 1.5 本章小结 第2章 XML应用简介 2.1 什么是XML应用程序 2.1.1 化学标记语言(Chemical Markup Langu 2.1.2 数学标记语言(Mathematical Markup La 2.1.3 频道定义格式 2.1.4 经典文学 2.2 用于XMLXML 2.2.1 XSL 2.2.2 XLL 2.2.3 DCD 2.3 XML的后台应用 2.4 本章小结 第3章 第一个XML文档 3.1 Hello XML 3.1.1 创建一个简单的XML文档 3.1.2 保存XML文件 3.1.3 将XML文件装入Web浏览器 .2 考察简单的XML文档 3.3 赋于XML标记以意义 .4 为XML文档编写样式单 .5 将样式单附加到XML文档上 3.6 本章小结 第4章 数据的结构化 4.1 检查数据 4.1.1 击球手 4.1.2 投球手 4.1.3 XML数据的组织 4.2 数据的XML化 4.2.1 开始编写文档: XML声明和根元素 4.2.2 联赛(League)、(分部) Division和 4.2.3 球员数据的XML化 4.2.4 球员统计数据的XML化 4.2.5 将XML组装在一起 4.3 XML格式的优点 4.4 编制样式单以便显示文档 4.4.1 与样式单连接 4.4.2 为根元素指定样式规则 4.4.3 为标题指定样式规则 4.4.4 为球员和统计元素指定样式规则 4.4.5 本节小结 4.5 本章小结 第5章 属性、空标记和XSL 5.1 属性 5.2 属性与元素的对比 5.2.1 结构化的元数据 5.2.2 元元数据 5.2.3 有关元数据的说明 5.2.4 元素更具扩展性 5.2.5 使用属性的最佳时机 5.3 空标记 5.4 XSL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值