]> git.armaanb.net Git - lightcards.git/blob - contrib/kvtml2html.py
Remove CursesError exception
[lightcards.git] / contrib / kvtml2html.py
1 #!/usr/bin/env python
2 # Converts .kvtml KWordQuiz files to HTML
3
4 import argparse
5 from bs4 import BeautifulSoup
6
7
8 def parse_args():
9     parser = argparse.ArgumentParser(
10         description="Convert KWordQuiz file into Markdown for Lightcards"
11     )
12     parser.add_argument("inp", metavar="input file", type=str, nargs=1)
13     parser.add_argument("outp", metavar="output file", type=str, nargs=1)
14     return parser.parse_args()
15
16
17 def main():
18     args = parse_args()
19     with open(args.inp[0], "r", encoding="utf-8") as input_file:
20         soup = BeautifulSoup(input_file, "lxml")
21
22     headers = [
23         x.get_text().split("\n")[1] for x in soup.find_all("identifier")
24     ]
25     body = soup.find_all("entry")
26     col1 = [x.find("translation", {"id": "0"}) for x in body]
27     col2 = [x.find("translation", {"id": "1"}) for x in body]
28
29     html = f"<html><table><tr><th>{headers[0]}</th><th>{headers[1]}</th></tr>"
30     for i in range(len(col1)):
31         try:
32             html += f"<html><table><tr><td>{col1[i].get_text().rstrip()}</td>"
33             html += f"<td>{col2[i].get_text().rstrip()}</td></tr>"
34         except:
35             pass
36
37     with open(args.outp[0], "w", encoding="utf-8") as output_file:
38         output_file.write(html)
39
40
41 if __name__ == "__main__":
42     main()