Metadata-Version: 2.4
Name: docx-book-typeset
Version: 1.0.0
Summary: Turn a Word manuscript into a retail EPUB 3 and a print-ready PDF
Author-email: Alessandro Catorcini <alessandro@catorcini.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/acato/docx-book-typeset
Project-URL: Documentation, https://github.com/acato/docx-book-typeset/blob/main/docs/README.md
Project-URL: Source, https://github.com/acato/docx-book-typeset
Project-URL: Issues, https://github.com/acato/docx-book-typeset/issues
Project-URL: Changelog, https://github.com/acato/docx-book-typeset/releases
Keywords: epub,epub3,typst,docx,word,typesetting,self-publishing,kdp,ingramspark,ebook,book,pdf
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: End Users/Desktop
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Printing
Classifier: Topic :: Text Processing :: Markup
Classifier: Topic :: Office/Business :: Office Suites
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: lxml>=5
Requires-Dist: PyYAML>=6
Requires-Dist: Pillow>=10
Requires-Dist: typst>=0.13
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pymupdf; extra == "test"
Requires-Dist: epubcheck; extra == "test"
Dynamic: license-file

# docx-book-typeset

Turn a Word manuscript (`.docx`) into a retail **EPUB 3** and a print-ready **PDF** interior, from one small
YAML file per edition. No desktop layout app, no proprietary fonts.

- **EPUB**: semantic XHTML, navigation document + NCX, landmarks, series metadata, optional cover,
  accessibility metadata. Passes [epubcheck](https://github.com/w3c/epubcheck) with no errors or warnings.
  No embedded fonts: readers use their own serif.
- **PDF**: laid out with [Typst](https://typst.app). Trim sizes 5x8, 5.25x8, 5.5x8.5, 6x9 and A5; mirrored
  margins with a gutter; chapters opening on a recto; running heads and folios (left off opening and blank
  pages); PDF bookmarks; optional half title and contents page. The default font is **Libertinus Serif**
  (SIL Open Font License), which ships inside Typst, so a build needs no font installation.

The look is deliberately plain: one serif family, centred headings, `* * *` scene breaks in print and a short
rule in the EPUB. No ornaments, no drop caps, no small-caps lead-ins.

![Side-by-side comparison of a Word document, the typeset PDF page, and the EPUB displayed on a phone.](https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/word-to-epub-and-pdf.png)

![Animation of the build command running in a terminal, followed by pages of the resulting PDF.](https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/build.gif)

![A two-page spread of the print PDF showing running heads and page numbers on opposite pages.](https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/print-spread.png)

## For authors: start here

**[The author's guide](docs/README.md)** walks you through everything step by step, no technical background
needed: installing, preparing the manuscript in Word, building the book, copy-and-paste settings for common
needs, and uploading to KDP, IngramSpark and the ebook stores.

The short version:

```sh
py -m pip install https://github.com/acato/docx-book-typeset/archive/refs/heads/main.zip   # once (Mac: python3)
typeset-book new      # answer a few questions -> book.yaml
typeset-book check    # see the chapters it found
typeset-book build    # -> build/My-Book.epub and build/My-Book-Print.pdf
```

## For developers

```sh
pip install -e ".[test]"      # lxml, PyYAML, Pillow, typst; pytest, pymupdf, epubcheck for the tests
pytest -q tests
```

If the `typst` command-line tool is on your `PATH` it is used; otherwise the `typst` Python package is.
Python 3.10 or newer. The modules can also be run one by one:

```sh
python examples/make_sample_docx.py              # writes examples/sample.docx
python -m typeset.docx2epub examples/sample.yaml # -> examples/build/The-Tide-Tables.epub
python -m typeset.docx2pdf  examples/sample.yaml # -> examples/build/The-Tide-Tables-Print.pdf
```

## How it compares

Vellum is limited to Mac, while Atticus is a paid app available on multiple operating systems. Calibre handles ebooks but is not a print layout tool. Reedsy Studio is a free, web-based editor where your book lives in their online interface. Pandoc and LaTeX setups are powerful but heavy to install.

docx-book-typeset accepts .docx files directly and outputs both retail EPUB 3 and print-ready PDF. It is free, open source, and runs on Windows, Mac, and Linux. There is no LaTeX, Pandoc, or font installation required. The design is deliberately plain and classic, reading Word styles rather than visual formatting. It is a command-line tool without a graphical interface, but it offers a simple, one-line installation for authors who want a clean, semantic output.

## Preparing the manuscript

The builders read the *styles* of the DOCX, not its visual formatting:

| In Word | Becomes |
|---|---|
| **Heading 1** | the start of a new element: a chapter, a part, or a front/back-matter section |
| **Subtitle** style right after a Heading 1 | the chapter (or part) subtitle |
| **Heading 2** / **Heading 3** | subheads inside a chapter (bold / italic) |
| **Quote** (or Intense Quote, Block Text) | block quote; consecutive Quote paragraphs form one quote |
| a paragraph that reads `* * *` | a scene break (configurable) |
| italic, bold, small caps, underline, strikethrough, super/subscript | kept |
| hyperlinks (external, internal to bookmarks, HYPERLINK and REF fields) | live links in EPUB and PDF |
| pictures placed *in line with text* | images (the picture's alt text is used in the EPUB) |

Anything before the first Heading 1 is ignored (with a warning): put the book's title and author in the YAML,
not at the top of the manuscript; the builders generate the title page.

Check what the builders will see with:

```sh
python -m typeset.docx_reader manuscript.docx          # headings, quotes, links, anchors, images
python -m typeset.docx_reader manuscript.docx --all    # every paragraph
```

## The edition file

One YAML per edition (book + language + format choices). Relative paths resolve against the YAML file.
A complete example is [`examples/sample.yaml`](examples/sample.yaml).

```yaml
docx: manuscript.docx

metadata:
  title: "The Tide Tables"            # required
  subtitle: "A Novel"                 # optional
  creator: "A. N. Author"             # required
  file_as: "Author, A. N."            # sort name, optional
  language: en-US                     # required (BCP 47)
  identifier: "urn:uuid:..."          # keep it stable across releases; derived from title/author if absent
  publisher: "Example Press"          # optional; also printed at the foot of the title page
  description: "..."                  # optional, EPUB only
  series: { name: "Harbour Books", position: 1, position_label: "Book One" }   # optional

default_type: chapter                 # type of any Heading 1 not listed in `elements`
elements:                             # exact Heading 1 text -> element type
  "Copyright": copyright
  "Dedication": dedication
  "Epigraph": epigraph
  "Part One": part
  "Prologue": prologue
  "Acknowledgments": acknowledgments
  "About the Author": about-author

chapter_heading: '^Chapter \d+: (?P<title>.+)$'   # optional: strip "Chapter N" from headings and keep the title;
                                                  # a regex without (?P<title>) makes the chapter title "Chapter N"
chapter_numbers: true                 # false: chapters show their title only
subtitle_style: Subtitle              # paragraph style used for chapter subtitles
scene_breaks: ["* * *"]               # paragraph texts that mean "scene break"

labels:                               # words the builders print; translate for other languages
  chapter: Chapter
  contents: Contents
  copyright: Copyright
  dedication: Dedication
  epigraph: Epigraph
  title_page: Title Page
  cover: Cover
  beginning: Start

epub:
  output: My-Book.epub                # default: <Title>.epub
  cover: cover.jpg                    # optional; re-encoded as JPEG, at most cover_width px wide (1600)
  contents_page: true                 # an HTML contents page after the opening front matter
  image_width: 1200                   # inline images are scaled down to this width

print:
  output: My-Book-Print.pdf           # default: <Title>-Print.pdf
  trim: 6x9                           # 5x8 | 5.25x8 | 5.5x8.5 | 6x9 | a5
  margins: { inside: 0.85, outside: 0.65, top: 0.8, bottom: 0.85 }   # inches; overrides the trim defaults
  font: "Libertinus Serif"            # a family name or a list (fallbacks)
  font_fallback: ["Noto Serif CJK SC"]   # appended after `font` (e.g. for a few CJK names in Latin text)
  font_paths: [fonts/]                # extra font folders (also: TYPST_FONT_PATHS)
  font_check: true                    # fail if the PDF does not embed `font` (see Fonts)
  font_size: 11                       # pt
  leading: 0.62                       # space between lines, in em
  indent: 1.5                         # paragraph indent, in em
  hyphenate: true
  chapter_start: recto                # recto | any
  running_heads: author-title         # author-title | title-chapter | none
  head_verso: "A. N. Author"          # defaults: author on the left page, title on the right
  head_recto: "The Tide Tables"
  half_title: false
  contents: false                     # a contents page with page numbers
  image_size: large                   # small | medium | large | full (share of the text width)
```

### Element types

| type | heading shown | notes |
|---|---|---|
| `chapter` | yes, numbered "Chapter N" (unless `chapter_numbers: false`) | numbered in order, whatever the DOCX says |
| `part` | yes, on a page of its own | |
| `copyright` | no | print: foot of the title page's verso |
| `dedication`, `epigraph` | no | print: centred on a recto |
| `none`, `foreword`, `preface`, `introduction`, `prologue`, `epilogue`, `afterword`, `acknowledgments`, `also-by`, `about-author` | yes, unnumbered | the type sets the EPUB semantics |

## Fonts

Print uses Libertinus Serif unless you name another family. The EPUB embeds no fonts at all, so a font
licence only matters for the print PDF.

Why Libertinus and not a public-domain font: there is no book-quality serif with a true italic whose
public-domain status is clear. The fonts sometimes described as public domain either lack an italic
(Goudy Bookletter 1911) or are distributed under the OFL or GUST licences in every usable OpenType form
(the Computer Modern descendants). The SIL Open Font License lets you embed the font in books you sell;
what it restricts is selling the font itself, or redistributing a modified version under the original name.

### Using a font you own a licence for

Two lines in the edition file swap the typeface; nothing in the code or the template changes.

1. **Put the font files in a folder** next to your edition file, for example `fonts/`. Use the desktop
   OpenType or TrueType files (`.otf` / `.ttf`), not web fonts. Include every style the book uses: regular
   and italic at least; bold if your manuscript has bold text or Heading 2 subheads. Typst does not fake a
   missing italic or bold.
2. **Point the edition file at it:**

   ```yaml
   print:
     font: "Minion Pro"          # the family name exactly as the font reports it
     font_paths: [fonts/]        # folder(s), relative to this YAML file
   ```

   The family name is the one your system's font manager shows (Font Book on macOS, the Fonts settings page
   on Windows, `fc-list : family` on Linux); with the Typst CLI, `typst fonts --font-path fonts/` lists
   exactly what the build will see.
3. **Build as usual.** After compiling, `docx2pdf` checks that the PDF really embeds the font you asked for.
   If the name is misspelled or the folder is wrong, Typst would quietly fall back to Libertinus, so
   the build stops instead and renames the PDF `*.WRONG-FONT.pdf`. If a font's internal name differs from its
   family name and the check fails wrongly, set `print.font_check: false`.

Before you publish, read the licence: most commercial desktop licences allow embedding a subset of the font in
a PDF for print (that is what the build does), but some do not, and some limit the number of computers.
**Keep licensed font files out of public repositories**: `fonts/` is in `.gitignore` for that reason. To
use the same font on several machines without copying it into the project, list its folder in the
`TYPST_FONT_PATHS` environment variable instead of `font_paths` (separated by `;` on Windows, `:` elsewhere).

You can also give a list, for example `font: ["Minion Pro", "Noto Serif CJK SC"]`, and the later families
are used for any characters the first one lacks.

## Notes for KDP / IngramSpark / Lulu

- The PDF has the exact trim size, with no bleed (text-only interiors do not need one) and all fonts embedded.
- The default gutter suits books up to roughly 500 pages. KDP asks for more inside margin as page count grows
  (0.625 in from 301 to 500 pages, 0.75 in from 501 to 700); raise `print.margins.inside` if needed.
- Store uploads of an ebook usually should not carry a print ISBN: keep the ISBN line in a separate DOCX
  (or edition) for print.

## Layout of the code

```
typeset/cli.py              the `typeset-book` command (new, check, build, sample)
typeset/docx_reader.py      DOCX -> paragraphs with styles, formatted runs, links, bookmarks, images
typeset/model.py            paragraphs -> elements and blocks (shared by both outputs); the edition file
typeset/docx2epub.py        EPUB 3 writer
typeset/epub/book.css       the EPUB stylesheet
typeset/docx2pdf.py         writes book.json and compiles templates/book.typ with Typst
typeset/templates/book.typ  the print layout
typeset/sample.py           the practice manuscript (`typeset-book sample`)
examples/                   the sample edition file
docs/                       the author's guide
tests/                      pytest tests, including one that loads every recipe in docs/4-recipes.md
```

## Books typeset with docx-book-typeset

Alessandro Catorcini has used docx-book-typeset for every English book he has published since The Master of the Seal.

<p>
<a href="https://www.catorcini.com/books/the-master-of-the-seal/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=the-master-of-the-seal"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/the-master-of-the-seal.jpg" width="100" alt="The Master of the Seal" title="The Master of the Seal"></a>
<a href="https://www.catorcini.com/books/the-master-of-the-ledger/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=the-master-of-the-ledger"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/the-master-of-the-ledger.jpg" width="100" alt="The Master of the Ledger" title="The Master of the Ledger"></a>
<a href="https://www.catorcini.com/books/the-stranger-in-the-cell/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=the-stranger-in-the-cell"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/the-stranger-in-the-cell.jpg" width="100" alt="The Stranger in the Cell" title="The Stranger in the Cell"></a>
<a href="https://www.catorcini.com/books/mapmakers-son-volume-i/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=mapmakers-son-volume-i"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/mapmakers-son-volume-i.jpg" width="100" alt="The Mapmaker's Son, Volume I" title="The Mapmaker's Son, Volume I"></a>
<a href="https://www.catorcini.com/books/the-debtors-letter/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=the-debtors-letter"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/the-debtors-letter.jpg" width="100" alt="The Debtor's Letter" title="The Debtor's Letter"></a>
<a href="https://www.catorcini.com/books/mapmakers-son-volume-ii/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=mapmakers-son-volume-ii"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/mapmakers-son-volume-ii.jpg" width="100" alt="The Mapmaker's Son, Volume II" title="The Mapmaker's Son, Volume II"></a>
<a href="https://www.catorcini.com/books/the-second-woman/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=the-second-woman"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/the-second-woman.jpg" width="100" alt="The Second Woman" title="The Second Woman"></a>
<a href="https://www.catorcini.com/books/the-merchants-will/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=the-merchants-will"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/the-merchants-will.jpg" width="100" alt="The Merchant's Will" title="The Merchant's Will"></a>
<a href="https://www.catorcini.com/books/the-agents-share/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=the-agents-share"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/the-agents-share.jpg" width="100" alt="The Agent's Share" title="The Agent's Share"></a>
<a href="https://www.catorcini.com/books/losing-the-north-star/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset&utm_content=losing-the-north-star"><img src="https://raw.githubusercontent.com/acato/docx-book-typeset/main/docs/images/showcase/losing-the-north-star.jpg" width="100" alt="Losing the North Star" title="Losing the North Star"></a>
</p>

If you have published a book using this tool, we would love to feature it. Please submit a pull request adding your cover image (under 300 px wide) to `docs/images/showcase/`, along with the title, author, and a link. There is no pressure; this list is simply a way to see the tool in action.

## License

Apache License 2.0, see [LICENSE](LICENSE). Libertinus Serif is distributed with Typst under the SIL Open Font License.

---

Made by Alessandro Catorcini to typeset his own historical novels. Learn more at [www.catorcini.com](https://www.catorcini.com/?utm_source=github&utm_medium=referral&utm_campaign=docx-typeset).
