当前位置: 首页 > news >正文

织梦分类信息做的网站长春seo培训

织梦分类信息做的网站,长春seo培训,软件工程最好的培训机构,章丘做网站公司请参阅:java : pdfbox 读取 PDF文件内书签 或者 python:从PDF中提取目录 请注意:书的目录.txt 编码:UTF-8,推荐用 Notepad 转换编码。 xml 是 python 标准库,在 D:\Python39\Lib\xml\etree pip install …

请参阅:java : pdfbox 读取 PDF文件内书签 或者 python:从PDF中提取目录

请注意:书的目录.txt 编码:UTF-8,推荐用 Notepad++ 转换编码。

xml 是 python 标准库,在 D:\Python39\Lib\xml\etree

pip install xmltodict ;

python 用 xml.etree.ElementTree,用 xmltodict 转换为json数据。

编写 txt_xml_etree_json.py  如下

# -*- coding: utf-8 -*-
""" 读目录.txt文件,用 xmltodict转换为json数据 """
import os
import sys
import codecs
import json
import xml.etree.ElementTree as et
import xmltodictif len(sys.argv) ==2:f1 = sys.argv[1]
else:print('usage: python txt_xml_etree_json.py file1.txt')sys.exit(1)if not os.path.exists(f1):print(f"ERROR: {f1} not found.")sys.exit(1)fn,ext = os.path.splitext(f1)
if ext.lower() != '.txt':print('ext is not .txt')sys.exit(2)fp = codecs.open(f1, mode="r", encoding="utf-8")
# 读取第一行:书名
title = fp.readline()
# 创建主题节点
root = et.Element("node")
root.set("id", '1')
root.set("text", title.strip())# 定义状态:
state = et.SubElement(root, "state")
state.set("opened", 'true')
state.set("disabled", 'true')# 用缩排表现层级关系,假设最多5个层级
indent1 = ' '*2
indent2 = ' '*4
indent3 = ' '*6
indent4 = ' '*8n = 2
for line in fp:txt = line.strip()if len(txt) ==0:continuetxt = txt[0:-3] # 去掉行尾的页数if len(txt) >0 and line[0] !=' ':# 创建主题的子节点(1级节点)node1 = et.SubElement(root, "children")node1.set("id", str(n))node1.set("text", txt)p_node = node1 # 寄存父节点elif line.startswith(indent1) and line[2] !=' ':# 创建node1的子节点(2级节点)try: type(node1)except NameError: node2 = et.SubElement(root, "children")else: node2 = et.SubElement(node1, "children")node2.set("id", str(n))node2.set("text", txt)p_node = node2elif line.startswith(indent2) and line[4] !=' ':# 创建node2的子节点(3级节点)try: type(node2)except NameError: node3 = et.SubElement(node1, "children")else: node3 = et.SubElement(node2, "children")node3.set("id", str(n))node3.set("text", txt)p_node = node3elif line.startswith(indent3) and line[6] !=' ':# 创建node3的子节点(4级节点)try: type(node3)except NameError: node4 = et.SubElement(node2, "children")else: node4 = et.SubElement(node3, "children")node4.set("id", str(n))node4.set("text", txt)p_node = node4elif line.startswith(indent4) and line[8] !=' ':# 创建node4的子节点(5级节点)try: type(node4)except NameError: node5 = et.SubElement(p_node, "children")else: node5 = et.SubElement(node4, "children")node5.set("id", str(n))node5.set("text", txt)else:print(txt)n += 1
fp.close()
print(f"line number: {n}")# 转换成 str,方便导出
root_bytes = et.tostring(root, encoding="utf-8")
xml_str = root_bytes.decode()
try:json_dict = xmltodict.parse(xml_str, encoding='utf-8')json_str = json.dumps(json_dict['node'], indent=2)
except:print("xmltodict.parse error!")
# 去掉'@'
json_str = '['+ json_str.replace('\"@','"') +']'
#print(json_str)# 导出.json文件
f2 = fn +'.json'
with codecs.open(f2, 'w', encoding='utf8') as fp:fp.write(json_str)

 python 用 xml.etree.ElementTree,用 xmltodict 转换为json数据,jinja2 生成jstree模板所需的文件。

编写 txt_xml_etree_htm.py  如下

# -*- coding: utf-8 -*-
""" 读目录.txt文件,用 xmltodict转换为json数据,生成jstree所需的文件 """
import os
import sys
import codecs
import json
import xml.etree.ElementTree as et
import xmltodict
from jinja2 import Environment,FileSystemLoaderif len(sys.argv) ==2:f1 = sys.argv[1]
else:print('usage: python txt_xml_etree_htm.py file1.txt')sys.exit(1)if not os.path.exists(f1):print(f"ERROR: {f1} not found.")sys.exit(1)fn,ext = os.path.splitext(f1)
if ext.lower() != '.txt':print('ext is not .txt')sys.exit(2)fp = codecs.open(f1, mode="r", encoding="utf-8")
# 读取第一行:书名
title = fp.readline()
# 创建主题节点
root = et.Element("node")
root.set("id", '1')
root.set("text", title.strip())# 定义状态:
state = et.SubElement(root, "state")
state.set("opened", 'true')
state.set("disabled", 'true')# 用缩排表现层级关系,假设最多5个层级
indent1 = ' '*2
indent2 = ' '*4
indent3 = ' '*6
indent4 = ' '*8n = 2
for line in fp:txt = line.strip()if len(txt) ==0:continuetxt = txt[0:-3] # 去掉行尾的页数if len(txt) >0 and line[0] !=' ':# 创建主题的子节点(1级节点)node1 = et.SubElement(root, "children")node1.set("id", str(n))node1.set("text", txt)p_node = node1 # 寄存父节点elif line.startswith(indent1) and line[2] !=' ':# 创建node1的子节点(2级节点)try: type(node1)except NameError: node2 = et.SubElement(root, "children")else: node2 = et.SubElement(node1, "children")node2.set("id", str(n))node2.set("text", txt)p_node = node2elif line.startswith(indent2) and line[4] !=' ':# 创建node2的子节点(3级节点)try: type(node2)except NameError: node3 = et.SubElement(node1, "children")else: node3 = et.SubElement(node2, "children")node3.set("id", str(n))node3.set("text", txt)p_node = node3elif line.startswith(indent3) and line[6] !=' ':# 创建node3的子节点(4级节点)try: type(node3)except NameError: node4 = et.SubElement(node2, "children")else: node4 = et.SubElement(node3, "children")node4.set("id", str(n))node4.set("text", txt)p_node = node4elif line.startswith(indent4) and line[8] !=' ':# 创建node4的子节点(5级节点)try: type(node4)except NameError: node5 = et.SubElement(p_node, "children")else: node5 = et.SubElement(node4, "children")node5.set("id", str(n))node5.set("text", txt)else:print(txt)n += 1
fp.close()
print(f"line number: {n}")# 转换成 str,方便导出
root_bytes = et.tostring(root, encoding="utf-8")
xml_str = root_bytes.decode()
try:json_dict = xmltodict.parse(xml_str, encoding='utf-8')json_str = json.dumps(json_dict['node'], indent=2)
except:print("xmltodict.parse error!")
# 去掉'@'
json_str = '['+ json_str.replace('\"@','"') +']'
#print(json_str)# 使用 jinja2 对html模板文件进行数据替换
env = Environment(loader=FileSystemLoader('d:/python/'))
tpl = env.get_template('jstree_template.htm')
# 导出.html文件
f2 = fn +'.htm'
with codecs.open(f2, 'w', encoding='utf8') as fp:content = tpl.render(title=title.strip(), mydir=json_str)fp.write(content)

https://gitee.com/ 搜索 jstree 下载
https://gitee.com/mirrors/jstree?_from=gitee_search
git clone https://gitee.com/mirrors/jstree.git

编写 jstree 模板文件:jstree_template.htm

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>{{title}}</title><script src="../js/jquery-3.2.1.min.js"></script><link rel="stylesheet" href="../js/jstree/dist/themes/default/style.css" /><script src="../js/jstree/dist/jstree.min.js"></script>
</head>
<body><!-- 搜索框 --><div class="search_input"><input type="text" id="search_a" /><img src="../js/jstree/dist/search.png" /></div><div id="treeview1" class="treeview"></div>
<script type="text/javascript">var mydir = {{mydir}};$("#treeview1").jstree({'core' : {"multiple" : false,'data' : mydir,'dblclick_toggle': true},"plugins" : ["search"]});//输入框输入时自动搜索var tout = false;$('#search_a').keyup(function(){if (tout) clearTimeout(tout);    tout = setTimeout(function(){$('#treeview1').jstree(true).search($('#search_a').val());   }, 250);});   
</script> 
</body>
</html>

运行 python txt_xml_etree_htm.py your_pdf_dir.txt

生成 your_pdf_dir.htm


文章转载自:
http://spoony.sqLh.cn
http://incalculably.sqLh.cn
http://imbed.sqLh.cn
http://plodding.sqLh.cn
http://cageling.sqLh.cn
http://anapestic.sqLh.cn
http://dw.sqLh.cn
http://ramrod.sqLh.cn
http://murrelet.sqLh.cn
http://enlightened.sqLh.cn
http://solonetz.sqLh.cn
http://teamster.sqLh.cn
http://hydrocephalic.sqLh.cn
http://dab.sqLh.cn
http://pryer.sqLh.cn
http://paddy.sqLh.cn
http://ophidiarium.sqLh.cn
http://preserval.sqLh.cn
http://turkic.sqLh.cn
http://moneybag.sqLh.cn
http://nartb.sqLh.cn
http://promptbook.sqLh.cn
http://tango.sqLh.cn
http://havelock.sqLh.cn
http://londonization.sqLh.cn
http://fishfag.sqLh.cn
http://whimsey.sqLh.cn
http://lacertian.sqLh.cn
http://truckie.sqLh.cn
http://before.sqLh.cn
http://subcellar.sqLh.cn
http://cuirass.sqLh.cn
http://avenger.sqLh.cn
http://conglobation.sqLh.cn
http://subserve.sqLh.cn
http://vergeboard.sqLh.cn
http://jaygee.sqLh.cn
http://volti.sqLh.cn
http://march.sqLh.cn
http://play.sqLh.cn
http://radiolucent.sqLh.cn
http://refloatation.sqLh.cn
http://bacchant.sqLh.cn
http://sistership.sqLh.cn
http://nessy.sqLh.cn
http://commemoratory.sqLh.cn
http://poriferous.sqLh.cn
http://platypi.sqLh.cn
http://sorel.sqLh.cn
http://superstrength.sqLh.cn
http://sycee.sqLh.cn
http://mora.sqLh.cn
http://nigerien.sqLh.cn
http://labionasal.sqLh.cn
http://itinerancy.sqLh.cn
http://assorted.sqLh.cn
http://dah.sqLh.cn
http://nhi.sqLh.cn
http://pompously.sqLh.cn
http://pedagogics.sqLh.cn
http://illusion.sqLh.cn
http://nelly.sqLh.cn
http://intendancy.sqLh.cn
http://caecostomy.sqLh.cn
http://beneficiate.sqLh.cn
http://vicereine.sqLh.cn
http://begar.sqLh.cn
http://leathercraft.sqLh.cn
http://dangerous.sqLh.cn
http://microbiology.sqLh.cn
http://commeasure.sqLh.cn
http://pyrographer.sqLh.cn
http://angiomatous.sqLh.cn
http://sittoung.sqLh.cn
http://perky.sqLh.cn
http://keelyvine.sqLh.cn
http://fixedness.sqLh.cn
http://cosmos.sqLh.cn
http://ordinance.sqLh.cn
http://illiterati.sqLh.cn
http://heelpiece.sqLh.cn
http://cameleer.sqLh.cn
http://republicanism.sqLh.cn
http://tenfold.sqLh.cn
http://curacao.sqLh.cn
http://stenciler.sqLh.cn
http://leaflet.sqLh.cn
http://decimation.sqLh.cn
http://engineering.sqLh.cn
http://quillwort.sqLh.cn
http://bunion.sqLh.cn
http://mnemotechnics.sqLh.cn
http://faun.sqLh.cn
http://degressively.sqLh.cn
http://stuff.sqLh.cn
http://thermoelectron.sqLh.cn
http://honorific.sqLh.cn
http://chinfest.sqLh.cn
http://itinerate.sqLh.cn
http://celloidin.sqLh.cn
http://www.15wanjia.com/news/57538.html

相关文章:

  • 武汉建站费用福州seo招聘
  • 网站制作web678厦门seo优化外包公司
  • 网站注册时间查询产品营销推广的方案
  • 二手交易网站开发方式开网站需要多少钱
  • 做网购网站要多少钱营销策划方案案例
  • 专做女鞋批发的网站网络营销的方法有哪些?
  • 郴州网站制作公司临沂头条新闻今日头条
  • 石碣做网站万网域名注册教程
  • 网站建设与制作百度资源共享
  • 美丽定制 网站模板优化设计答案大全英语
  • sem网站做推广磁力搜索引擎torrentkitty
  • 哪些网站可以做宣传东莞seo广告宣传
  • 校园失物招领网站建设江苏seo和网络推广
  • 著名的深圳网站建设网站推广怎样做
  • 湖州市网站建设0元入驻的电商平台
  • 编写html的软件长沙正规竞价优化服务
  • 人人做全免费网站网店推广的重要性
  • 企业网站公众号软件测试培训
  • 怎么敲代码做网站发稿服务
  • 网站开发dede南宁seo专员
  • 机票网站制作sem是指什么
  • 我想建一个做私彩的网站seo网站编辑是做什么的
  • 合肥公司做网站线上推广
  • 网站建设备案方案中国优秀网页设计案例
  • 做详情页到那个网站找模特素材网店运营基础知识
  • 茶叶网站的建设策划书关键词优化排名软件怎么样
  • 建公司网站的详细步骤无锡网络推广外包
  • go语言可以做网站吗百度优化大师
  • 挪车码推广赚钱站长工具seo综合查询源码
  • 做模具做什么网站浙江网站推广运营