-
Notifications
You must be signed in to change notification settings - Fork 0
/
terms_exporter.py
53 lines (49 loc) · 1.79 KB
/
terms_exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os.path
import sys
import docx
from tqdm import tqdm
class TermsExporter:
@classmethod
def doc_export(cls, terms: list, lang: str):
if lang.lower() == "рус":
filename = "docs/glossary_ru.docx"
else:
filename = "docs/glossary_kz.docx"
print(f"Экспорт в {filename}...")
for i in range(len(terms)):
terms[i] = terms[i].split(" - ", 1)
doc = docx.Document()
doc.add_heading("Глоссарий", 0)
table = doc.add_table(rows=1, cols=2)
table.style = "Table Grid"
header_cells = table.rows[0].cells
if lang.lower() == "рус":
header_cells[0].text = "Термин"
header_cells[1].text = "Значение"
else:
header_cells[0].text = "Термин"
header_cells[1].text = "Терминнің мағынасы"
i = 0
term = ""
description = ""
try:
for term, description in tqdm(terms):
row_cells = table.add_row().cells
row_cells[0].text = term
row_cells[1].text = description
i += 1
except:
print(f"Exception occurred: {sys.exc_info()[0]}")
print(f"Term: {term} Description: {description} I: {i}")
print(terms[i])
doc.save(filename)
@classmethod
def export_terms_to_txt(cls, selected_terms: list, lang: str):
if lang.lower() == "рус":
txt_filename = "docs/glossary_ru.txt"
else:
txt_filename = "docs/glossary_kz.txt"
print(f"Экспорт в {txt_filename}...")
with open(txt_filename, "w", encoding="UTF-8") as glossaryFile:
for term in selected_terms:
glossaryFile.write(term)