zenivan commited on
Commit
e7958d4
·
1 Parent(s): a9d8471
Files changed (1) hide show
  1. app/main_WS_FS_pure_test.py +27 -44
app/main_WS_FS_pure_test.py CHANGED
@@ -604,51 +604,34 @@ with gr.Blocks(theme=theme, css_paths=css_path) as demo:
604
  error_para.add_run(html.unescape(error_text))
605
 
606
 
607
- def generate_docx(html_content: str) -> str:
608
- # Предварительная обработка
609
- html_content = re.sub(r"\*\*(.+?)\*\*", r"<p><strong>\1</strong></p>", html_content)
610
- soup = BeautifulSoup(html_content, 'html.parser')
611
-
612
- # Создаем документ
 
 
 
 
 
 
613
  doc = Document()
614
- section = doc.sections[0]
615
- section.orientation = WD_ORIENT.LANDSCAPE
616
- section.page_width, section.page_height = section.page_height, section.page_width
617
-
618
- # Инициализация конвертера
619
- converter = HtmlToDocx()
620
- converter.table_style = "Table Grid"
621
-
622
- # Обработка основных блоков
623
- header = soup.find('section', class_='header-block')
624
- tables = soup.find_all('table')
625
- materials = soup.find('div', class_='reference-block')
626
-
627
- # Конвертация шапки
628
- if header:
629
- safe_convert(converter, header, doc, "в шапке")
630
-
631
- # Конвертация таблиц
632
- for table in tables:
633
- # Упрощаем структуру таблицы
634
- for tr in table.find_all('tr'):
635
- for td in tr.find_all('td'):
636
- td.attrs = {}
637
- td.name = 'td' # Принудительно устанавливаем тег
638
-
639
- safe_convert(converter, table, doc, "в таблице")
640
-
641
- # Конвертация дополнительных материалов
642
- if materials:
643
- safe_convert(converter, materials, doc, "в материалах")
644
-
645
- # Сохранение файла
646
- temp_dir = tempfile.gettempdir()
647
- filename = f"lesson_plan_{datetime.now():%Y%m%d%H%M%S}.docx"
648
- filepath = os.path.join(temp_dir, filename)
649
- doc.save(filepath)
650
-
651
- return filepath
652
 
653
 
654
  ################### СТРИМИНГ
 
604
  error_para.add_run(html.unescape(error_text))
605
 
606
 
607
+ def generate_docx(html: str) -> str:
608
+ # Упрощаем HTML перед обработкой
609
+ html = re.sub(r'class="[^"]+"', '', html) # Удаляем все классы
610
+ html = re.sub(r'<br/?>', '\n', html) # Заменяем <br> на переносы
611
+ html = re.sub(r'<strong>(.*?)</strong>', r'\1', html) # Убираем bold
612
+
613
+ # Удаляем проблемные атрибуты
614
+ soup = BeautifulSoup(html, 'html.parser')
615
+ for tag in soup.find_all(True):
616
+ tag.attrs = {}
617
+
618
+ # Конвертируем
619
  doc = Document()
620
+ parser = HtmlToDocx()
621
+
622
+ try:
623
+ parser.add_html_to_document(str(soup), doc)
624
+ except Exception as e:
625
+ # При ошибке вставляем сырой текст
626
+ doc.add_paragraph("ОШИБКА КОНВЕРТАЦИИ:")
627
+ doc.add_paragraph(soup.get_text())
628
+
629
+ # Сохраняем файл
630
+ tmp = tempfile.gettempdir()
631
+ fname = f"plan_{datetime.now():%Y%m%d%H%M%S}.docx"
632
+ path = os.path.join(tmp, fname)
633
+ doc.save(path)
634
+ return path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
 
636
 
637
  ################### СТРИМИНГ