Download authoring/export_docx.py from garywelz/shadow: direct link, hf CLI and curl.
- Browser
- Download file 869 Bytes
-
https://huggingface.co/spaces/garywelz/shadow/resolve/main/authoring/export_docx.py
- Command line
-
hf download hf://spaces/garywelz/shadow/authoring/export_docx.py
-
curl -L -o export_docx.py https://huggingface.co/spaces/garywelz/shadow/resolve/main/authoring/export_docx.py
869 Bytes
| 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() | |