-
Couldn't load subscription status.
- 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/
βββ scripts/
βββ create_chapters.py
π Script path: ./scripts/create_chapters.py
If no --project-dir is provided, files are created under:
./manuscript/chapters/
-
Place
create_chapters.pyinscripts/. -
Register the CLI entry in
pyproject.toml:
[tool.poetry.scripts]
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
- 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)
Use this sidebar to navigate all key workflows β from setup to translation, export, and testing.