Spaces:
Running on Zero
Running on Zero
| from __future__ import annotations | |
| from pathlib import Path | |
| from ocr_studio.language import contains_arabic_script | |
| def write_docx_file(text: str, destination: Path, language: str) -> Path: | |
| from docx import Document | |
| from docx.enum.text import WD_ALIGN_PARAGRAPH | |
| from docx.oxml import OxmlElement | |
| document = Document() | |
| rtl = language in {"fa", "fa+en"} or contains_arabic_script(text) | |
| lines = (text or "").splitlines() or [""] | |
| for block in lines: | |
| paragraph = document.add_paragraph(block) | |
| if not rtl: | |
| continue | |
| paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT | |
| p_pr = paragraph._p.get_or_add_pPr() | |
| bidi = OxmlElement("w:bidi") | |
| p_pr.append(bidi) | |
| document.save(str(destination)) | |
| return destination | |