Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- from lxml import etree as ET
- import zipfile as ZIP
- import sys
- import os
- namespaces = { "opf":"http://www.idpf.org/2007/opf",
- "dc":"http://purl.org/dc/elements/1.1/",
- "ncx":"http://www.daisy.org/z3986/2005/ncx/"
- }
- info = {}
- def sortncx(file):
- ncx = parseNCX(file)
- navpoints = ncx.xpath(".//ncx:navPoint",namespaces=namespaces)
- counter = 1
- for item in navpoints:
- item.set("playOrder",str(counter))
- counter += 1
- else:
- counter = 1
- ncx = ET.ElementTree(ncx)
- return ET.tostring(ncx)
- def parseInfo(file):
- global info
- try:
- f = ZIP.ZipFile(file).read("META-INF/container.xml")
- except KeyError:
- print("The %s file is not a valid OCF." % str(file))
- try:
- m = ET.fromstring(f)
- info["path_to_opf"] = m[0][0].get("full-path")
- root_folder = os.path.dirname(info["path_to_opf"])
- except:
- pass
- opf = ET.fromstring(ZIP.ZipFile(file).read(info["path_to_opf"]))
- id = opf.xpath("//opf:spine",namespaces=namespaces)[0].get("toc")
- expr = "//*[@id='%s']" % id
- info["ncx_name"] = opf.xpath(expr)[0].get("href")
- info["path_to_ncx"] = root_folder + "/" + info["ncx_name"]
- info.pop("ncx_name")
- return info
- def parseNCX(file):
- ncx = {}
- ncx = ET.fromstring(ZIP.ZipFile(file).read(parseInfo(file)["path_to_ncx"]))
- return ncx
- if __name__ == '__main__':
- info = parseInfo(sys.argv[1])
- sorted_ncx = sortncx(sys.argv[1])
- epub = ZIP.ZipFile(sys.argv[1],"a")
- epub.writestr(info["path_to_ncx"],sorted_ncx)
- epub.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement