File size: 816 Bytes
98e1d9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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