Browse Source

Работа с документами переделана на библиотеку weasyprint #9

auth_koop
Alexei 1 year ago
parent
commit
3e6d3a5ec5
  1. 120
      auth_docs/test_doc.html
  2. BIN
      auth_docs/test_doc.odt
  3. 2
      bot_modules/authorize.py
  4. 2
      config_auth_docs
  5. 2
      requirements.txt
  6. 60
      template/docs_message.py

120
auth_docs/test_doc.html

@ -0,0 +1,120 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>
<meta name="generator" content="LibreOffice 7.3.6.2 (Linux)"/>
<meta name="created" content="00:00:00"/>
<meta name="changed" content="2023-10-31T15:50:17.206169759"/>
<style type="text/css">
@page { size: 21cm 29.7cm; margin: 2cm }
p { line-height: 115%; margin-bottom: 0.25cm; background: transparent }
td p { orphans: 0; widows: 0; background: transparent }
a:link { color: #000080; so-language: zxx; text-decoration: underline }
a:visited { color: #800000; so-language: zxx; text-decoration: underline }
</style>
</head>
<body lang="ru-RU" link="#000080" vlink="#800000" dir="ltr"><p style="line-height: 100%; margin-bottom: 0cm">
<br/>
</p>
<p align="center" style="line-height: 100%; margin-bottom: 0cm"><font size="5" style="font-size: 18pt"><b>Регистрационные
данные</b></font></p>
<p style="line-height: 100%; margin-bottom: 0cm"><br/>
</p>
<p style="line-height: 100%; margin-bottom: 0cm"><br/>
</p>
<table width="616" cellpadding="0" cellspacing="0" style="page-break-before: auto; page-break-after: auto">
<col width="305"/>
<col width="311"/>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>ID пользователя</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_ID
</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Имя</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_NAME</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Фамилия</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_FAMILY_NAME</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Отчество</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_MIDDLE_NAME</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Дата рождения</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_BIRTHDAY</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Адрес</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_ADDRESS</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Контакты</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_CONTACTS</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Подтверждение</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.USER_CONFIRM</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Доступ</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.ACCESS</p>
</td>
</tr>
<tr valign="top">
<td width="305" style="border: none; padding: 0cm"><p align="justify">
<b>Дата создания записи</b></p>
</td>
<td width="311" style="border: none; padding: 0cm"><p align="justify">
TableFieldDestiny.CREATE_DATE</p>
</td>
</tr>
</table>
<p style="line-height: 100%; margin-bottom: 0cm"><br/>
</p>
</body>
</html>

BIN
auth_docs/test_doc.odt

Binary file not shown.

2
bot_modules/authorize.py

@ -332,7 +332,7 @@ def GetReplaceDictFunc(a_Bot, a_user_id):
result = {}
i = 0
for f in table.GetFields():
result.update({str(f.m_Destiny): item[i]})
result.update({str(f.m_Destiny): str(item[i])})
i += 1
print(result)
return result

2
config_auth_docs

@ -1 +1 @@
auth_docs/test_doc.odt
auth_docs/test_doc.html

2
requirements.txt

@ -1,3 +1,3 @@
aiogram==2.20
colorama==0.4.5
odfpy
weasyprint

60
template/docs_message.py

@ -40,35 +40,49 @@ def DocFilesTemplate(a_Bot, a_FilesFunc, a_CaptionMessage, a_AccessFunc, a_GetBu
)
return DocFiles
from odf import opendocument, text, teletype
async def ReplaceInFile(a_Bot, a_InputFileName, a_OutFileName, a_DictReplace):
try:
filedata = ''
print('ReplaceInFile f', a_InputFileName)
with open(a_InputFileName, 'r') as in_f:
filedata = in_f.read()
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id):
print ('MakeDocFile', a_FilePath)
#FixBadZipfile(a_FilePath)
document = opendocument.load(a_FilePath) # odf.opendocuement.
if document == None:
a_Bot.GetLog().Error(f'Не удалось загрузить файл {a_FilePath}.')
return None
s = filedata
print('ReplaceInFile s', s)
for rep_this, to_this in a_DictReplace.items():
print('ReplaceInFile', rep_this, to_this)
s = s.replace(rep_this, to_this)
for element in document.getElementsByType(text.Span):
extracted_text = teletype.extractText(element)
print('MakeDocFile', extracted_text)
with open(a_OutFileName, 'w') as out_f:
out_f.write(s)
a_Bot.GetLog().Success(f'Создан файл {a_OutFileName}')
return a_OutFileName
except Exception as e:
a_Bot.GetLog().Error(f'Не удалось заменить текст в фале {a_InputFileName} и записать в {a_OutFileName}. Ошибка {str(e)}')
return None
for rep_this, to_this in a_DictReplace.items():
if extracted_text.find(rep_this) != -1:
extracted_text = extracted_text.replace(rep_this, to_this)
from weasyprint import HTML, CSS
new_element = text.Span()
new_element.setAttribute('stylename', element.getAttribute('stylename'))
new_element.addText(extracted_text)
async def SaveAsPdf(a_Bot, a_InputFileName, a_OutFileName):
try:
HTML(filename = a_InputFileName).write_pdf(a_OutFileName)
a_Bot.GetLog().Success(f'Создан файл {a_OutFileName}')
return a_OutFileName
except Exception as e:
a_Bot.GetLog().Error(f'Не удалось создать пдф из фала {a_InputFileName} и записать в {a_OutFileName}. Ошибка {str(e)}')
return None
element.parentNode.insertBefore(new_element, element)
element.parentNode.removeChild(element)
async def MakeDocFile(a_Bot, a_FilePath, a_DictReplace, a_user_id):
print ('MakeDocFile', a_FilePath)
user_file_path = a_FilePath[:-5]
pdf_file_path = user_file_path
user_file_path += f"_{a_user_id}.html"
pdf_file_path += f"_{a_user_id}.pdf"
user_file_path = await ReplaceInFile(a_Bot, a_FilePath, user_file_path, a_DictReplace)
if not user_file_path:
return None
new_file_path = a_FilePath[:-4]
new_file_path += f"_{a_user_id}.odt"
document.save(new_file_path)
return new_file_path
return await SaveAsPdf(a_Bot, user_file_path, pdf_file_path)
async def GetFile(a_Bot, a_Path):
if not a_Path:

Loading…
Cancel
Save