""" =============================================================================== FILE: services/office/word_builder.py PROJECT: SCUD Orion AI (Office Domain) ROLE: Сборка форматированного документа MS Word (.docx) из распознанного текста. =============================================================================== """ import os from typing import List, Dict, Any from docx import Document from docx.shared import Pt, Inches from docx.enum.text import WD_ALIGN_PARAGRAPH def build_docx_from_ocr(pages_data: List[Dict[str, Any]], output_filepath: str, doc_title: str = "Распознанный документ") -> str: """ Создает файл .docx по ГОСТ-стандартам делопроизводства: - Шрифт Times New Roman 12-14pt. - Межстрочный интервал 1.15. - Разделители страниц и колонтитулы. """ doc = Document() sections = doc.sections for section in sections: section.top_margin = Inches(0.79) section.bottom_margin = Inches(0.79) section.left_margin = Inches(0.79) section.right_margin = Inches(0.59) for p_idx, page in enumerate(pages_data): page_num = page.get("page", p_idx + 1) text_content = page.get("text", "") if p_idx > 0: doc.add_page_break() lines = text_content.splitlines() for line in lines: line_str = line.strip() if not line_str: continue p = doc.add_paragraph() p.paragraph_format.space_after = Pt(3) p.paragraph_format.line_spacing = 1.15 if any(h in line_str.upper() for h in ["ПРЕДСЕДАТЕЛЬСТВОВАЛ", "ПРОТОКОЛ", "ПОВЕСТКА", "РЕШИЛИ:", "ОТМЕТИЛИ:"]): p.alignment = WD_ALIGN_PARAGRAPH.CENTER run = p.add_run(line_str) run.font.name = "Times New Roman" run.font.size = Pt(13) run.font.bold = True else: p.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY run = p.add_run(line_str) run.font.name = "Times New Roman" run.font.size = Pt(12) os.makedirs(os.path.dirname(output_filepath), exist_ok=True) doc.save(output_filepath) return output_filepath