> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Notion 数据库 2/2 集成

> 使用 LangChain Python 与 Notion 数据库 2/2 文档加载器集成。

> [Notion](https://www.notion.so/) 是一个支持修改版 Markdown 的协作平台，集成了看板、任务、维基和数据库。它是一个集笔记记录、知识与数据管理、项目和任务管理于一体的全方位工作空间。

`NotionDBLoader` 是一个用于从 `Notion` 数据库加载内容的 Python 类。它从数据库中检索页面，读取其内容，并返回一个 Document 对象列表。`NotionDirectoryLoader` 用于从 Notion 数据库导出文件中加载数据。

## 要求

* 一个 `Notion` 数据库
* Notion 集成令牌

## 设置

### 1. 创建 Notion 表格数据库

在 Notion 中创建一个新的表格数据库。你可以向数据库添加任意列，它们将被视为元数据。例如，你可以添加以下列：

* 标题：将“标题”设为默认属性。
* 分类：一个多选属性，用于存储与页面关联的分类。
* 关键词：一个多选属性，用于存储与页面关联的关键词。

将你的内容添加到数据库中每个页面的正文部分。NotionDBLoader 将从这些页面中提取内容和元数据。

## 2. 创建 Notion 集成

要创建 Notion 集成，请按照以下步骤操作：

1. 访问 [Notion 开发者](https://www.notion.com/my-integrations) 页面，并使用你的 Notion 账户登录。
2. 点击“+ 新建集成”按钮。
3. 为你的集成命名，并选择数据库所在的工作区。
4. 选择所需的能力，本扩展仅需要“读取内容”能力。
5. 点击“提交”按钮以创建集成。
   集成创建后，你将获得一个 `集成令牌（API 密钥）`。复制此令牌并妥善保管，因为使用 NotionDBLoader 时需要它。

### 3. 将集成连接到数据库

要将你的集成连接到数据库，请按照以下步骤操作：

1. 在 Notion 中打开你的数据库。
2. 点击数据库视图右上角的三个点菜单图标。
3. 点击“+ 新建集成”按钮。
4. 找到你的集成，你可能需要在搜索框中输入其名称。
5. 点击“连接”按钮，将集成连接到数据库。

### 4. 获取数据库 ID

要获取数据库 ID，请按照以下步骤操作：

1. 在 Notion 中打开你的数据库。
2. 点击数据库视图右上角的三个点菜单图标。
3. 从菜单中选择“复制链接”，将数据库 URL 复制到剪贴板。
4. 数据库 ID 是 URL 中的一长串字母数字字符。它通常看起来像这样：[www.notion.so/username/8935f9d140a04f95a872520c4f123456?v=](https://www.notion.so/username/8935f9d140a04f95a872520c4f123456?v=).... 在这个例子中，数据库 ID 是 8935f9d140a04f95a872520c4f123456。

正确设置数据库并获取集成令牌和数据库 ID 后，你现在可以使用 NotionDBLoader 代码从你的 Notion 数据库加载内容和元数据。

### 5. 安装

安装 `langchain-community` 集成包。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-community
```

## Notion 数据库加载器

NotionDBLoader 是 langchain 包中文档加载器的一部分。你可以按如下方式使用它：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from getpass import getpass

NOTION_TOKEN = getpass()
DATABASE_ID = getpass()
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
········
········
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import NotionDBLoader
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
loader = NotionDBLoader(
    integration_token=NOTION_TOKEN,
    database_id=DATABASE_ID,
    request_timeout_sec=30,  # 可选，默认为 10
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docs = loader.load()
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(docs)
```

## Notion 目录加载器

### 设置

从 Notion 导出你的数据集。你可以通过点击右上角的三个点，然后点击 `导出` 来完成。

导出时，请确保选择 `Markdown & CSV` 格式选项。

这将在你的下载文件夹中生成一个 `.zip` 文件。将 `.zip` 文件移动到此仓库中。

运行以下命令解压 zip 文件（根据需要将 `Export...` 替换为你自己的文件名）。

```shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
unzip Export-d3adfe0f-3131-4bf3-8987-a52017fc1bae.zip -d Notion_DB
```

### 使用

运行以下命令来加载你刚刚下载的数据。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import NotionDirectoryLoader

loader = NotionDirectoryLoader("Notion_DB")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docs = loader.load()
```

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\document_loaders\notion.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
