-
Notifications
You must be signed in to change notification settings - Fork 2
Chapter File Generator
This tool helps you automatically create empty Markdown files for chapters (or scenes, parts, lessons—your choice) in your book project. It is designed to work with a typical structure like:
your-project/
├── manuscript/
│ └── chapters/
│ ├── 01-chapter.md
│ ├── 02-chapter.md
│ └── ...
The script is written in Python, designed for use with Poetry, and includes a command-line interface for convenience.
-
✅ Flexible filenames via
--name-pattern(e.g.{num:02d}-scene.md,{num}-part.md) -
✅ Autodetects the next number based on your chosen pattern
-
✅ Optional manual start via
--start -
✅ Dry-run mode to preview without writing files
-
✅ Ensures the
manuscript/chapters/directory exists -
✅ Clean Poetry CLI integration
-
✅ Works with default or custom project root
ℹ️ The
--name-patternmust include a{num}placeholder (with optional formatting like:02d).
Examples:{num:02d}-chapter.md,{num:03d}_scene.md,{num}-part.md
By default, the script operates on this structure:
<project-root>/
├── manuscript/
│ └── chapters/
📌 Command: poetry run create-chapters
If no --project-dir is provided, files are created under:
./manuscript/chapters/
-
Ensure
manuscriptais installed viapoetry install. -
Register the CLI entry in
pyproject.toml:
the manuscripta library
create-chapters = "scripts.create_chapters:main"- Run
poetry installif needed.
You can now invoke it from anywhere inside your project:
poetry run create-chapterspoetry run create-chapters --total 5🟢 Output: Files like 03-chapter.md … 07-chapter.md (starting after the highest existing)
📁 Location: ./manuscript/chapters/
poetry run create-chapters --start 3 --total 7🟢 Sample output:
✓ Created: .../manuscript/chapters/03-chapter.md
✓ Created: .../manuscript/chapters/04-chapter.md
...
✓ Created: .../manuscript/chapters/09-chapter.md
poetry run create-chapters --project-dir my-book --total 3🟢 Output: Files created inside my-book/manuscript/chapters/
poetry run create-chapters --total 3 --name-pattern "{num:02d}-scene.md"Creates:
01-scene.md
02-scene.md
03-scene.md
poetry run create-chapters --total 2 --start 10 --name-pattern "{num:03d}_part.md"Creates:
010_part.md
011_part.md
poetry run create-chapters --total 5 --dry-run --name-pattern "{num}-lesson.md"Shows what would be created without touching the filesystem.
-
Builds the target directory:
<project-root>/manuscript/chapters/(or under--project-dir). -
Parses your
--name-patternand converts it to a regex so it can detect existing files that match the pattern. -
If
--startis omitted, it finds the highest existing number that matches the pattern and continues from there; otherwise it uses your--start. -
Creates empty Markdown files using your pattern (e.g.,
{num:02d}-chapter.md→03-chapter.md).
🔐 Safety: Existing files are not overwritten—
touch(exist_ok=True)is used.
poetry run create-chapters --total 10Creates:
01-chapter.md … 10-chapter.md
poetry run create-chapters --total 3If the last file is 07-chapter.md, it adds:
08-chapter.md … 10-chapter.md
poetry run create-chapters --total 2 --start 20Creates:
20-chapter.md
21-chapter.md
-
The pattern must contain
{num}(optionally formatted like{num:02d}). -
To include a literal brace in your pattern, double it (e.g.,
{{or}}) due tostr.formatrules. -
Non-matching files in the folder are ignored during autodetection.
-
Use
--dry-runto validate your pattern and the planned filenames before creation.
A pytest suite covers:
-
default vs. custom project dirs
-
autodetection of the next number based on your pattern
-
explicit
--startbehavior -
dry-run behavior
-
pattern validation (missing
{num}raises an error)
The script is open for reuse and adaptation under your project’s license.
- Home
- Project Initialization
- Generate Project Structure
- How to Write a Book
- Developer Workflow & Makefile
- Chapter File Generator
- Generate Images
- Convert Markdown Images
- Bulk Change File Extensions
- Restructure Chapters
- Remove Bold from Markdown Headers
- Converting Markdown Bullets to Typographic Bullets
- Translate Markdown with DeepL
- Translate with LM Studio
- Translation CLI Commands
- Shortcuts for Translation
- Automatic Book Export
- Shortcuts for Export
- Export HTML Chapters (Comics)
- Export to EPUB 2
- Pandoc Batch Processor
- Export HTML Books to PDF (KDP Ready)
**Libraries: ** manuscripta | manuscript-tools