2022-08-27 07:38:54 +03:00
|
|
|
import os
|
2022-08-28 12:37:28 +03:00
|
|
|
import re
|
|
|
|
import convertapi
|
|
|
|
|
2022-08-27 07:38:54 +03:00
|
|
|
|
|
|
|
from checker.services.generators import generate_charset
|
|
|
|
|
|
|
|
|
2022-08-27 11:59:23 +03:00
|
|
|
def _base_process(text):
|
2022-08-26 23:18:02 +03:00
|
|
|
paragraphs = {}
|
2022-08-27 10:16:21 +03:00
|
|
|
c = 1
|
|
|
|
title = True
|
2022-08-26 23:18:02 +03:00
|
|
|
for line in text:
|
2022-08-27 10:16:21 +03:00
|
|
|
if title:
|
|
|
|
if line and len(line) > 2 and line[:2] == "1.":
|
|
|
|
title = False
|
|
|
|
else:
|
|
|
|
if line:
|
|
|
|
paragraphs[c] = line
|
|
|
|
c += 1
|
2022-08-27 07:38:54 +03:00
|
|
|
return paragraphs
|
|
|
|
|
|
|
|
|
2022-08-27 11:59:23 +03:00
|
|
|
def process_paragraphs(text):
|
|
|
|
text = text.split("\n")
|
|
|
|
return _base_process(text)
|
|
|
|
|
|
|
|
|
|
|
|
def process_word_paragraphs(text):
|
|
|
|
text = text.split("\\r")
|
|
|
|
return _base_process(text)
|
|
|
|
|
|
|
|
|
2022-08-28 12:37:28 +03:00
|
|
|
def doc_to_docx(file_path):
|
|
|
|
convertapi.api_secret = '0fp22XFRPwKmNJql'
|
|
|
|
result = convertapi.convert('docx', {'File': file_path}, from_format='doc')
|
|
|
|
result.file.save(file_path.split(".")[0] + ".docx")
|
|
|
|
return file_path.split(".")[0] + ".docx"
|
|
|
|
|
|
|
|
|
|
|
|
def doc_to_odt(file_path):
|
|
|
|
convertapi.api_secret = '0fp22XFRPwKmNJql'
|
|
|
|
result = convertapi.convert('docx', {'File': file_path}, from_format='odt')
|
|
|
|
result.file.save(file_path.split(".")[0] + ".docx")
|
|
|
|
return file_path.split(".")[0] + ".docx"
|
|
|
|
|
|
|
|
|
2022-08-27 07:38:54 +03:00
|
|
|
def media_upload_path(instance, filename):
|
|
|
|
return os.path.join(f"uploads/{generate_charset(7)}/", filename)
|
2022-08-28 12:37:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
def split_text(text):
|
|
|
|
texts, groups = [], []
|
|
|
|
regt = re.findall(r"{(.*?)}(.*?){(.*?)}", text.replace('\n', ''))
|
|
|
|
for t in regt:
|
|
|
|
if t[0] == t[-1]:
|
|
|
|
texts.append(t[1])
|
|
|
|
groups.append(int(t[0]))
|
|
|
|
else:
|
|
|
|
print(t)
|
|
|
|
return texts, groups
|