Advertisement
andreymal

fb2_convert_annotations.py

Jan 6th, 2020
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import sys
  6. from typing import Any
  7.  
  8. import lxml.etree
  9.  
  10.  
  11. ns = {"fb": "http://www.gribuser.ru/xml/fictionbook/2.0"}
  12.  
  13.  
  14. def add_em_for_annotation(annotation: Any) -> None:
  15.     for p in annotation.xpath(".//fb:p", namespaces=ns):
  16.         em = lxml.etree.Element("{http://www.gribuser.ru/xml/fictionbook/2.0}emphasis")
  17.         em.text = p.text
  18.         p.text = ''
  19.         for x in p.getchildren():
  20.             em.append(x)
  21.         p.append(em)
  22.  
  23.  
  24. def fix_fb2_file(path: str) -> None:
  25.     fname = os.path.split(path)[-1]
  26.     print(fname, end=": ", flush=True)
  27.  
  28.     fb2 = lxml.etree.parse(path)
  29.  
  30.     for annotation in fb2.xpath(".//fb:section/fb:annotation", namespaces=ns):
  31.         add_em_for_annotation(annotation)
  32.         # annotation.getparent().remove(annotation)
  33.  
  34.     name, ext = os.path.splitext(path)
  35.     path_fixed = name + " - fixed annotations" + ext
  36.     fb2.write(path_fixed, xml_declaration=True, encoding="utf-8")
  37.     print("OK", flush=True)
  38.  
  39.  
  40. def main() -> int:
  41.     for f in sys.argv[1:]:
  42.         fix_fb2_file(f)
  43.     return 0
  44.  
  45.  
  46. if __name__ == "__main__":
  47.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement