Spaces:
Sleeping
Sleeping
Update converter.py
Browse files- converter.py +24 -0
converter.py
CHANGED
|
@@ -190,6 +190,8 @@ class MarkdownToDocxConverter:
|
|
| 190 |
p = self.doc.add_paragraph()
|
| 191 |
for child in element.children:
|
| 192 |
self.process_html_element(child, p)
|
|
|
|
|
|
|
| 193 |
elif element.name == 'pre':
|
| 194 |
code_element = element.find('code')
|
| 195 |
if code_element:
|
|
@@ -269,6 +271,28 @@ class MarkdownToDocxConverter:
|
|
| 269 |
for run in paragraph.runs:
|
| 270 |
run.bold = True
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
def convert(self, markdown_text: str, output_path: str):
|
| 273 |
"""Конвертация Markdown текста в DOCX файл"""
|
| 274 |
# Создаем новый документ
|
|
|
|
| 190 |
p = self.doc.add_paragraph()
|
| 191 |
for child in element.children:
|
| 192 |
self.process_html_element(child, p)
|
| 193 |
+
elif element.name == 'a':
|
| 194 |
+
self.add_hyperlink(parent_paragraph, element.get_text(), element.get('href'))
|
| 195 |
elif element.name == 'pre':
|
| 196 |
code_element = element.find('code')
|
| 197 |
if code_element:
|
|
|
|
| 271 |
for run in paragraph.runs:
|
| 272 |
run.bold = True
|
| 273 |
|
| 274 |
+
def add_hyperlink(self, paragraph, text, url):
|
| 275 |
+
"""Добавление гиперссылки"""
|
| 276 |
+
part = paragraph.part
|
| 277 |
+
r_id = part.relate_to(url, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink', is_external=True)
|
| 278 |
+
|
| 279 |
+
hyperlink = OxmlElement('w:hyperlink')
|
| 280 |
+
hyperlink.set(qn('r:id'), r_id)
|
| 281 |
+
|
| 282 |
+
new_run = OxmlElement('w:r')
|
| 283 |
+
rPr = OxmlElement('w:rPr')
|
| 284 |
+
|
| 285 |
+
# Стиль для гиперссылки
|
| 286 |
+
style = OxmlElement('w:rStyle')
|
| 287 |
+
style.set(qn('w:val'), 'Hyperlink')
|
| 288 |
+
rPr.append(style)
|
| 289 |
+
|
| 290 |
+
new_run.append(rPr)
|
| 291 |
+
new_run.text = text
|
| 292 |
+
hyperlink.append(new_run)
|
| 293 |
+
|
| 294 |
+
paragraph._p.append(hyperlink)
|
| 295 |
+
|
| 296 |
def convert(self, markdown_text: str, output_path: str):
|
| 297 |
"""Конвертация Markdown текста в DOCX файл"""
|
| 298 |
# Создаем новый документ
|