Notion to Markdown Exporter

2.9.0 · active · verified Fri Apr 17

notion2md is a Python CLI tool designed to export Notion pages, databases, and blocks into markdown files. It leverages the Notion API client to fetch content and convert it into a standard markdown format. The current version is 2.9.0, and the project has an active, though irregular, release cadence with recent updates.

Common errors

Warnings

Install

Imports

Quickstart

The primary use case for notion2md is via its command-line interface. This quickstart demonstrates how to export a Notion page by setting the `NOTION_TOKEN` environment variable and executing the `notion2md` command with a page ID and an output directory. Ensure your Notion API token has the necessary read permissions.

import os
import subprocess

# Set your Notion API token as an environment variable
# Ensure the token has read access to the page/database you intend to export.
# Example: export NOTION_TOKEN="secret_YOUR_NOTION_TOKEN"
# For quick testing, you can set it directly (NOT recommended for production):
# os.environ['NOTION_TOKEN'] = 'secret_YOUR_NOTION_TOKEN'

# Replace with your actual Notion Page ID
NOTION_PAGE_ID = os.environ.get('NOTION_PAGE_ID', 'a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6')

if not os.environ.get('NOTION_TOKEN'):
    print("Error: NOTION_TOKEN environment variable is not set.")
    print("Please set it (e.g., export NOTION_TOKEN='secret_YOUR_TOKEN')")
else:
    print(f"Attempting to export Notion Page ID: {NOTION_PAGE_ID}...")
    try:
        # Example CLI usage: Export a Notion page to a local 'output' directory
        result = subprocess.run(
            ['notion2md', NOTION_PAGE_ID, '-o', 'output'],
            capture_output=True, text=True, check=True
        )
        print("Export successful!")
        print("STDOUT:", result.stdout)
        if result.stderr:
            print("STDERR:", result.stderr)
    except subprocess.CalledProcessError as e:
        print(f"Error during export: {e}")
        print("STDOUT:", e.stdout)
        print("STDERR:", e.stderr)
    except FileNotFoundError:
        print("Error: 'notion2md' command not found. Please ensure the package is installed and in your PATH.")

view raw JSON →