|
|
@ -6,7 +6,7 @@ |
|
|
|
from bot_sys import log, config, user_access |
|
|
|
from bot_sys import log, config, user_access |
|
|
|
from bot_modules import groups_utils |
|
|
|
from bot_modules import groups_utils |
|
|
|
from template import simple_message |
|
|
|
from template import simple_message |
|
|
|
import odf |
|
|
|
#import odf |
|
|
|
|
|
|
|
|
|
|
|
def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_ErrorMessage, access_mode = user_access.AccessMode.EDIT): |
|
|
|
def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetButtonsFunc, a_GetInlineButtonsFunc, a_ErrorMessage, access_mode = user_access.AccessMode.EDIT): |
|
|
|
async def DocFiles(a_Message): |
|
|
|
async def DocFiles(a_Message): |
|
|
@ -23,11 +23,11 @@ def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetBu |
|
|
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups) |
|
|
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups) |
|
|
|
return |
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
for file_path, dict_replace in files.values(): |
|
|
|
for file_path, dict_replace in files.items(): |
|
|
|
if not dict_replace: |
|
|
|
if not dict_replace: |
|
|
|
continue |
|
|
|
continue |
|
|
|
new_file = await MakeDocFile(a_Bot, file_path, dict_replace, user_id) |
|
|
|
new_file = await MakeDocFile(a_Bot, file_path, dict_replace, user_id) |
|
|
|
document = await GetFile(a_Bot, file_path) |
|
|
|
document = await GetFile(a_Bot, new_file) |
|
|
|
if document is None: |
|
|
|
if document is None: |
|
|
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups) |
|
|
|
await simple_message.SendMessage(a_Bot, a_ErrorMessage, a_GetButtonsFunc, None, user_id, a_Message, user_groups) |
|
|
|
else: |
|
|
|
else: |
|
|
@ -40,20 +40,25 @@ def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetBu |
|
|
|
) |
|
|
|
) |
|
|
|
return DocFiles |
|
|
|
return DocFiles |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from odf import opendocument, text, teletype |
|
|
|
|
|
|
|
|
|
|
|
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id): |
|
|
|
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id): |
|
|
|
document = odf.opendocument.load(a_FilePath) |
|
|
|
print ('MakeDocFile', a_FilePath) |
|
|
|
|
|
|
|
#FixBadZipfile(a_FilePath) |
|
|
|
|
|
|
|
document = opendocument.load(a_FilePath) # odf.opendocuement. |
|
|
|
if document == None: |
|
|
|
if document == None: |
|
|
|
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_FilePath}.') |
|
|
|
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_FilePath}.') |
|
|
|
return None |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
for element in document.getElementsByType(text.Span): |
|
|
|
for element in document.getElementsByType(text.Span): |
|
|
|
extracted_text = odf.teletype.extractText(element) |
|
|
|
extracted_text = teletype.extractText(element) |
|
|
|
|
|
|
|
print('MakeDocFile', extracted_text) |
|
|
|
|
|
|
|
|
|
|
|
for file_path, dict_replace in a_DictReplace.values(): |
|
|
|
for rep_this, to_this in a_DictReplace.items(): |
|
|
|
if extracted_text.find('Replace this') != -1: |
|
|
|
if extracted_text.find(rep_this) != -1: |
|
|
|
extracted_text = extracted_text.replace('Replace this', 'to this') |
|
|
|
extracted_text = extracted_text.replace(rep_this, to_this) |
|
|
|
|
|
|
|
|
|
|
|
new_element = odf.text.Span() |
|
|
|
new_element = text.Span() |
|
|
|
new_element.setAttribute('stylename', element.getAttribute('stylename')) |
|
|
|
new_element.setAttribute('stylename', element.getAttribute('stylename')) |
|
|
|
new_element.addText(extracted_text) |
|
|
|
new_element.addText(extracted_text) |
|
|
|
|
|
|
|
|
|
|
@ -61,7 +66,7 @@ async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id): |
|
|
|
element.parentNode.removeChild(element) |
|
|
|
element.parentNode.removeChild(element) |
|
|
|
|
|
|
|
|
|
|
|
new_file_path = a_FilePath[:-4] |
|
|
|
new_file_path = a_FilePath[:-4] |
|
|
|
new_file_path += f"{a_user_id}.odt" |
|
|
|
new_file_path += f"_{a_user_id}.odt" |
|
|
|
document.save(new_file_path) |
|
|
|
document.save(new_file_path) |
|
|
|
return new_file_path |
|
|
|
return new_file_path |
|
|
|
|
|
|
|
|
|
|
|