Notion to Markdown Exporter
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
-
ModuleNotFoundError: No module named 'notion2md'
cause The 'notion2md' package is not installed in your current Python environment.fixRun `pip install notion2md` to install the package. -
notion2md: command not found
cause The `notion2md` executable is not in your system's PATH, or the package was not installed with its entry points accessible.fixEnsure `pip install notion2md` completed successfully. If using a virtual environment, ensure it is activated. You might need to add your Python scripts directory to your system's PATH. -
Error during export: Command '['notion2md', 'invalid-id', '-o', 'output']' returned non-zero exit status 1. STDOUT: ... Error: Invalid id. Please provide a valid Notion block, page, or database ID.
cause The provided Notion ID is either incorrect, malformed, or does not exist, or the Notion integration does not have access to it.fixDouble-check the Notion ID for typos. Ensure it's a valid Block ID, Page ID, or Database ID. Verify that your Notion API token's integration has been explicitly granted access to the page/database in Notion. -
Error: Authentication error: Invalid API token. STDOUT: ...
cause The `NOTION_TOKEN` environment variable is either missing, incorrect, or the token is expired/invalid.fixVerify that `NOTION_TOKEN` is set correctly in your environment (e.g., `echo $NOTION_TOKEN`). Regenerate the token in Notion if necessary and update your environment variable.
Warnings
- breaking Version 2.0.0 was a complete rewrite of the library, introducing significant breaking changes. Code written for versions prior to 2.0.0 will not be compatible.
- gotcha The `NOTION_TOKEN` environment variable must be set correctly, and the associated Notion Integration must have the necessary read permissions for the specific pages or databases you are trying to export.
- gotcha notion2md relies on the Notion API. Changes in the official Notion API or its client libraries (e.g., `notion-client-py`) can lead to unexpected behavior or breakage in notion2md until the library is updated.
- gotcha Not all Notion block types are perfectly supported or rendered in markdown. Complex blocks (e.g., sync blocks, advanced tables, some embedded content) might not export as expected or might be omitted.
Install
-
pip install notion2md
Imports
- notion_to_markdown
from notion2md.exporter import notion_to_markdown
Quickstart
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.")