Debian Codegraph @

python/examples/translate-html.py

raw symbols SCIP
1 #! /usr/bin/python -u
2 #
3 # Copyright (C) 2020 Jake Kyle <[email protected]>
4 #
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved. This file is offered as-is,
8 # without any warranty.
9
10 """Convert an HTML file to Braille
11
12 This is a tiny example that illustrates how you can use lxml to read
13 HTML and then translate only the text contained in the HTML nodes.
14
15 Similar scripts can be written for XML in general using lxml.etree.
16 Also there is much fine grained control possible by selecting
17 different elements and attributes more specifically, using xpath and
18 other methods available within lxml, to do different things
19 """
20
21 import textwrap
22
23 import louis
24 from lxml import html
25
26 tableList = ["en-ueb-g2.ctb"]
27 lineLength = 38
28 fileIn = input("Please enter the input file name: ")
29 fileOut = input("Please enter the output file name: ")
30
31 with open(fileOut, "w") as outputFile:
32 html_root = html.parse(fileIn).getroot()
33 for head_or_body in html_root:
34 for elem in head_or_body:
35 if elem.xpath("string()").strip() != "":
36 line = elem.xpath("string()")
37 translation = louis.translateString(tableList, line, 0, 0)
38 outputFile.write(textwrap.fill(translation, lineLength))
39 outputFile.write("\n")
40
41 print("Done.")