File size: 869 Bytes
b15141d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
from __future__ import annotations

from io import BytesIO
from typing import Any


def export_docx(manuscript: dict[str, Any]) -> bytes:
    from docx import Document
    from docx.shared import Pt

    doc = Document()
    title = manuscript.get("title") or "Manuscript"
    doc.add_heading(title, level=0)

    chapters = manuscript.get("chapters") or []
    for idx, ch in enumerate(chapters, start=1):
        ch_title = ch.get("title") or f"Chapter {idx}"
        doc.add_heading(ch_title, level=1)
        for seg in ch.get("segments") or []:
            text = (seg.get("text") or "").strip()
            if not text:
                continue
            for para in text.split("\n\n"):
                p = doc.add_paragraph(para.strip())
                p.paragraph_format.space_after = Pt(10)

    buf = BytesIO()
    doc.save(buf)
    return buf.getvalue()