Skip to main content

Convert Python docstrings to Markdown documentation.

Project description

PyDoc2Markdown

CI codecov PyPI Python versions License

Convert Python docstrings into clean, structured Markdown documentation.

PyDoc2Markdown turns Python docstrings into plain Markdown that is easy to read, easy to commit, and easy to publish anywhere. Point it at your source tree and it can generate module docs, a navigation-ready docs directory, or a maintained API section inside your README.

It is built for projects that want documentation to stay close to the code without taking on a full documentation framework. The output works well on GitHub, GitLab, MkDocs, and any other Markdown renderer.

What you get:

  • Plain .md files instead of a custom docs runtime
  • Useful output with almost no setup
  • A CLI for day-to-day use and a small library API for automation
  • Features that help docs stay current: --check, --prune, README sync, and navigation pages

A good fit for:

  • Python libraries that want lightweight API docs in the repo
  • Internal tools and services that keep docs next to the code
  • Projects that want README API sections without manual copy-paste
  • Teams that prefer Markdown files over framework-specific documentation stacks

Table of Contents

Why PyDoc2Markdown?

Documentation generators like Sphinx are powerful but often come with noticeable setup: themes, conf.py, extensions, and custom builders to get the exact output you want. pdoc and mkdocstrings are lighter, but they still pull you toward a broader documentation stack.

PyDoc2Markdown takes a different approach: minimal setup, no framework lock-in, plain Markdown output. Point it at a Python project and get structured docs that work anywhere: GitHub, GitLab, MkDocs, or any Markdown renderer.

  • No conf.py - works out of the box
  • No framework lock-in - generates plain .md files
  • Minimal dependencies - Jinja2 + docstring-parser
  • Practical defaults - useful CLI output before you write any config

Features

  • Docstring parsing - Extract Google and NumPy style docstrings, with basic reStructuredText field support via docstring-parser.
  • Markdown generation - Produce clean Markdown files with customizable Jinja2 templates.
  • README API sections - Create or update a generated API reference block in README files.
  • Auto-generated index & TOC - Each module gets a Table of Contents; an index.md with package grouping is created automatically.
  • Navigation layout - Generate a docs entrypoint with package pages and API files under api/.
  • CI-friendly checks - Verify generated docs are up to date with --check.
  • Managed cleanup - Preview or remove stale generated Markdown with --prune and --dry-run.
  • Module filtering - Include or exclude source files with glob patterns.
  • Parameter defaults - Show which function arguments are required or optional.
  • Source links - Link generated classes, functions, and methods back to source code.
  • Package grouping - Output files are organized into subdirectories matching the package structure.
  • Built-in themes - Choose between default (detailed) and minimal themes, or supply your own template.
  • CLI & API - Use via command line or import as a Python library.
  • Recursive processing - Scan entire packages in one command.
  • Type-aware - Respects type hints and annotations.
  • Advanced constructs - Supports property, classmethod, staticmethod, dataclass, Enum, TypedDict, Protocol, ABC, Pydantic, and __all__.

Requirements

Installation

# Base installation
pip install pydoc2markdown

# With file watcher support
pip install pydoc2markdown[watch]

Visual Tour

Here is the kind of navigation-first Markdown output the project generates from the included sample package:

Generated docs showcase

Try It In 30 Seconds

Clone the repository and run PyDoc2Markdown against the included sample project:

pydoc2markdown examples/sample_project/src --recursive --nav --readme \
  --readme-path examples/sample_project/README.md \
  -o examples/sample_project/docs

That one command updates the sample project's README API block and creates a navigation-first docs layout you can inspect immediately:

examples/sample_project/
|-- README.md
|-- src/shop_demo/
`-- docs/
    |-- index.md
    |-- shop_demo.md
    `-- api/shop_demo/
        |-- inventory.md
        `-- orders.md

Sample Project

The sample project is a tiny shop package with dataclasses, an enum, typed functions, properties, and Google-style docstrings. It is the fastest way to understand what PyDoc2Markdown actually produces in a real repo layout.

You can inspect the whole workflow side by side:

You can also create the same kind of demo project locally:

pydoc2markdown --demo

By default this writes to pydoc2markdown-demo/. Use --demo-output to choose another directory. Existing non-empty directories are not overwritten.

Before And After

This is the same idea in a tighter input -> output view:

Input to output example

Start with normal Python code and docstrings:

def calculate_total(items: list[Product], discount: float = 0.0) -> float:
    """Calculate the discounted order total.

    Args:
        items: Products to include in the total.
        discount: Discount ratio between 0 and 1.

    Returns:
        Total price after discount.

    Raises:
        ValueError: If discount is outside the accepted range.
    """

PyDoc2Markdown turns it into Markdown with headings, parameter tables, return types, and raised exceptions:

### `calculate_total`

Calculate the discounted order total.

**Parameters:**

| Name | Type | Description |
|------|------|-------------|
| `items` | `list[Product]` | Products to include in the total. |
| `discount` | `float` | Discount ratio between 0 and 1. |

**Returns:** `float`
Total price after discount.

**Raises:**
- `ValueError`: If discount is outside the accepted range.

Common Commands

Start with the command that matches how you want to publish docs:

Goal Command
Generate module docs pydoc2markdown src/my_package --recursive -o docs
Generate a docs index and API pages pydoc2markdown src/my_package --recursive --nav -o docs
Update the API section in README.md pydoc2markdown src/my_package --recursive --readme
Skip private/internal modules pydoc2markdown src/my_package --recursive --exclude "tests/*,*/internal/*,*_private.py"
Add GitHub source links pydoc2markdown src/my_package --recursive --source-repo user/repo -o docs
Check generated docs in CI pydoc2markdown src/my_package --recursive --nav --readme --check -o docs
Preview stale generated docs cleanup pydoc2markdown src/my_package --recursive --prune --dry-run -o docs
Remove stale generated docs pydoc2markdown src/my_package --recursive --prune -o docs
Generate one combined Markdown file pydoc2markdown src/my_package --recursive --single-file -o docs/api.md
Watch source files while editing pydoc2markdown src/my_package --recursive --watch -o docs
Create default pyproject config pydoc2markdown --init
Create a local demo project pydoc2markdown --demo

Use --theme minimal for shorter output, or --template path/to/template.md.j2 when a project needs custom Markdown.

Recipes

Here are a few practical ways teams tend to use PyDoc2Markdown.

Keep API docs in docs/

Generate a navigation-ready docs tree for a package:

pydoc2markdown src/my_package --recursive --nav -o docs

This works well when you want browsable Markdown pages in the repository, MkDocs, or another lightweight docs site.

Keep a README API block in sync

Update a generated API section without touching the rest of the README:

pydoc2markdown src/my_package --recursive --readme --readme-path README.md

This is a good fit for small libraries that want usage notes and API reference in one file.

Gate docs in CI

Fail CI when generated docs are missing, stale, or no longer match the source tree:

pydoc2markdown src/my_package --recursive --nav --readme --check -o docs

Pair this with --prune in local development to keep generated docs tidy after renames and deletions.

Generate docs only for the public surface

Focus on selected packages and skip internal modules:

pydoc2markdown src/my_package --recursive \
  --include "api/*,models/*" \
  --exclude "*/internal/*,*_test.py" \
  -o docs

This is useful when your source tree is larger than the documentation surface you actually want to publish.

Quick Start

CLI Usage

# Generate docs for a single file
pydoc2markdown my_module.py -o docs

# Recursively process a package
pydoc2markdown src/my_package --recursive -o docs

# Skip internal and test modules
pydoc2markdown src/my_package --recursive --exclude "tests/*,*/internal/*,*_test.py" -o docs

# Generate a navigation-first docs layout
pydoc2markdown src/my_package --recursive --nav -o docs

# Add GitHub source links next to documented objects
pydoc2markdown src/my_package --recursive --source-repo user/repo -o docs

# Check whether generated docs are current without writing files
pydoc2markdown src/my_package --recursive --nav --check -o docs

# Preview stale generated Markdown files before removing them
pydoc2markdown src/my_package --recursive --prune --dry-run -o docs

# Remove stale generated Markdown files tracked by PyDoc2Markdown
pydoc2markdown src/my_package --recursive --prune -o docs

# Initialize default configuration in pyproject.toml
pydoc2markdown --init

Library Usage

from pathlib import Path
from pydoc2markdown import DocstringParser, MarkdownGenerator

parser = DocstringParser()
modules = parser.parse(Path("src/my_package"), recursive=True)

generator = MarkdownGenerator(theme="default")
generator.generate(modules, output_dir=Path("docs"))

CLI Reference

Flag Default Description
source (required unless --init is used) Path to a Python file or directory to process
--init False Create or update [tool.pydoc2markdown] in pyproject.toml
-o, --output docs / value from pyproject.toml Output directory (or file path when --single-file is used)
--recursive False / value from pyproject.toml Recursively process subdirectories
--include None Comma-separated glob patterns for files to include
--exclude None Comma-separated glob patterns for files to exclude
--theme default / value from pyproject.toml Built-in theme: default (detailed) or minimal
--template None Path to a custom Jinja2 template for Markdown generation
--single-file False Generate a single combined Markdown file; --output must be a .md or .markdown file path
--check False Check whether generated docs are up to date without writing files
--prune False Remove stale generated Markdown files tracked by PyDoc2Markdown
--dry-run False Preview --prune results without deleting files
--readme False Create or update an API reference section in README.md
--readme-path README.md Path to the README file updated by --readme
--nav False Generate a navigation-first docs layout with API pages under api/
--api-dir api Directory for API pages when --nav is used
--source-link None URL template for source links, using {path}, {file}, and {line}
--source-repo None GitHub repository shorthand for source links, for example user/repo
--watch False Watch source files and regenerate docs on change
--demo False Create a small demo project and generate docs for it
--demo-output pydoc2markdown-demo Directory created by --demo
-v, --verbose 0 Increase verbosity (-v = INFO, -vv = DEBUG)
--version - Show version and exit

Configuration priority: CLI flags > [tool.pydoc2markdown] in pyproject.toml > built-in defaults.

Configuration

You can initialize default configuration with:

pydoc2markdown --init

This creates or appends a [tool.pydoc2markdown] section in pyproject.toml. If the section already exists, it is left unchanged.

You can also set default values manually in your pyproject.toml:

[tool.pydoc2markdown]
output = "docs"
theme = "default"
recursive = true

Any values set here serve as defaults and can be overridden by CLI flags.

Module Filtering

Use --include and --exclude with --recursive to control which Python files are documented:

pydoc2markdown src/my_package --recursive --exclude "tests/*,*/internal/*,*_private.py"

Patterns use standard shell-style globs and are matched against paths relative to the scanned source root. Patterns without a directory separator also match file names in any package directory, so conftest.py or *_test.py work as convenient basename filters.

When both flags are used, PyDoc2Markdown applies --include first and then removes anything matched by --exclude:

pydoc2markdown src/my_package --recursive --include "api/*,core/*" --exclude "*/generated.py"

Source Links

Use --source-repo to add GitHub source links next to generated class, function, and method headings:

pydoc2markdown src/my_package --recursive --source-repo user/repo -o docs

This expands to a GitHub URL template using the main branch:

https://github.com/user/repo/blob/main/{path}#L{line}

For GitLab, another branch, or a custom host, pass the full template with --source-link:

pydoc2markdown src/my_package --recursive \
  --source-link "https://gitlab.com/user/repo/-/blob/develop/{path}#L{line}" \
  -o docs

Available template variables are {path} for the source-root-relative Python file path, {file} for the filename, and {line} for the 1-indexed definition line.

README API Sections

Use --readme to create or update a compact API reference in your README:

pydoc2markdown src/my_package --recursive --readme

By default, PyDoc2Markdown updates README.md. Use --readme-path to target a different file:

pydoc2markdown src/my_package --recursive --readme --readme-path docs/index.md

When the file already contains PyDoc2Markdown markers, only the generated block between the markers is replaced:

## API Reference

<!-- pydoc2markdown:start -->
<!-- pydoc2markdown:end -->

If the markers are missing, a new ## API Reference section is appended. If the README does not exist, it is created.

Navigation Docs Layout

Use --nav when you want a docs directory that is ready to browse from a single entrypoint:

pydoc2markdown src/my_package --recursive --nav -o docs

This creates a layout like:

docs/
|-- index.md
|-- modules.md
`-- api/
    |-- package.md
    `-- utils.md

The root index.md links to package landing pages and every generated API page. Use --api-dir to change where module pages are written:

pydoc2markdown src/my_package --recursive --nav --api-dir reference -o docs

CI Checks

Use --check in CI to fail when generated documentation is missing or stale:

pydoc2markdown src/my_package --recursive --nav --readme --check -o docs

The command compares the files PyDoc2Markdown would generate with the files already on disk. It exits with 0 when docs are current and 1 when any generated output needs to be updated. It does not write files in check mode.

--check works with normal multi-file output, --nav, --single-file, and README API sections. It cannot be combined with --watch.

Prune Stale Docs

Use --prune to remove stale generated Markdown files that were tracked by PyDoc2Markdown in a previous run but are no longer expected for the current source tree:

pydoc2markdown src/my_package --recursive --prune -o docs

To preview the cleanup first, add --dry-run:

pydoc2markdown src/my_package --recursive --prune --dry-run -o docs

--prune only affects generated Markdown files recorded in the PyDoc2Markdown manifest. It does not try to delete unrelated hand-written Markdown files that may live in the same docs directory.

Library API

DocstringParser

from pathlib import Path
from pydoc2markdown import DocstringParser

parser = DocstringParser()
modules = parser.parse(Path("src/my_package"), recursive=True)

MarkdownGenerator

from pathlib import Path
from pydoc2markdown import DocstringParser, MarkdownGenerator

# Parse modules
parser = DocstringParser()
modules = parser.parse(Path("src/my_package"), recursive=True)

# Default theme, separate files
gen = MarkdownGenerator(theme="default")
gen.generate(modules, output_dir=Path("docs"))

# Minimal theme
gen_min = MarkdownGenerator(theme="minimal")
gen_min.generate(modules, output_dir=Path("docs_minimal"))

# Custom template
gen_tmpl = MarkdownGenerator(template_path=Path("my_template.md.j2"))
gen_tmpl.generate(modules, output_dir=Path("docs_custom"))

# Single combined file
gen.generate_single_file(modules, output_path=Path("docs/README.md"))

# README API section
gen.update_readme(modules, readme_path=Path("README.md"))

# Navigation docs layout
gen.generate_navigation(modules, output_dir=Path("docs"))

# Markdown string for a single module
md_string = gen.generate_string(modules[0])

Supported Docstring Formats

PyDoc2Markdown uses docstring-parser and supports common structured docstring sections:

Style Support Notes
Google Full Args, Returns, Raises, Attributes, Examples
NumPy Full Parameters, Returns, Raises, Attributes, Examples
reStructuredText (reST) Basic Field-style metadata via docstring-parser, including :param:, :returns:, and :raises:

Example Output

Given this source file:

from pydantic import BaseModel, Field


class User(BaseModel):
    """A user in the system."""

    id: int = Field(description="Unique identifier")
    email: str = Field(default="", description="Email address")
    is_active: bool = True


class UserService:
    """Service for managing users."""

    def get_user(self, user_id: int) -> User:
        """Fetch a user by ID.

        Args:
            user_id: The user's unique identifier.

        Returns:
            The requested User instance.

        Raises:
            ValueError: If the user does not exist.
        """
        ...

Running pydoc2markdown src/users.py -o docs produces:

# users

## Table of Contents

- [Classes](#classes)
  - [`User`](#user)
  - [`UserService`](#userservice)

## Classes

### `User` *(Pydantic)*

A user in the system.

#### Pydantic Fields

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `id` | `int` | *required* | Unique identifier |
| `email` | `str` | `""` | Email address |
| `is_active` | `bool` | `True` | - |

### `UserService`

Service for managing users.

#### Methods

##### `get_user`

Fetch a user by ID.

**Parameters:**

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `user_id` | `int` | *required* | The user's unique identifier. |

**Returns:** `User`

**Raises:**

- `ValueError`: If the user does not exist.

For a complete small project, see examples/sample_project/. It includes source code, a generated README API section, and a navigation-first docs layout generated by PyDoc2Markdown. You can also see pre-built documentation for this repository in examples/docs/.

Documentation

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydoc2markdown-0.6.3.tar.gz (205.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydoc2markdown-0.6.3-py3-none-any.whl (33.2 kB view details)

Uploaded Python 3

File details

Details for the file pydoc2markdown-0.6.3.tar.gz.

File metadata

  • Download URL: pydoc2markdown-0.6.3.tar.gz
  • Upload date:
  • Size: 205.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pydoc2markdown-0.6.3.tar.gz
Algorithm Hash digest
SHA256 5b5a45739f7f738612ebe7120324754d9b8e746cb7054467b0326a276ca969e3
MD5 74c0c9636b064733059a84cebaae0edc
BLAKE2b-256 fc1db27c8b8fa166d1a45f8f1729708c16e98bc0adfc71cbb8d0ed15a4e4c68e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydoc2markdown-0.6.3.tar.gz:

Publisher: release.yml on f1sherFM/PyDoc2Markdown

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydoc2markdown-0.6.3-py3-none-any.whl.

File metadata

  • Download URL: pydoc2markdown-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 33.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pydoc2markdown-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5c5637d7cdb77561a8f383e2fe2ea6cb80c572d3a0c654ef0c1fc3889d1971bb
MD5 1119af3fd8f5d263a9f7dd44df632659
BLAKE2b-256 5be6a55b6b8b76d5da7aa066ae1c48a219a7f0464a00a25ba9b9ad4e4697f7ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydoc2markdown-0.6.3-py3-none-any.whl:

Publisher: release.yml on f1sherFM/PyDoc2Markdown

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page