-
Notion API Python SDK is a list of basic scripts to upload and update blocks
to Notion using Python. -
Scripts are based on notion-sdk-py by Guillaume Gelin (ramnes),
which manages the communication with Notion through its API. -
This repository adds the following functionalities:
- ✔️ An easy way to build blocks supported by the Notion API as well as
text formatting for those blocks. - ✔️ Provide a markdown parser to create Notion blocks.
- ✔️ Simply functionality to upload external files to Dropbox, extract
their raw shared link URL to be used in blocks such asimage
,video
,
file
,embed
or any that supports adding external links.
(Please note that Notion API does not allow to upload files directly.)
- ✔️ An easy way to build blocks supported by the Notion API as well as
- Notion requirements
- Notion blocks for Python
- Additional Notion SDK functionalities
- Markdown parser
- Dropbox requirements
- Dropbox functionalities
Install the python package notion-sdk-py created by Guillaume Gelin (ramnes) by running:
pip install notion-client
- or - pythonX.Y -m pip install notion-client
(where X.Y
correspond to the Python version you want to install the package in).
- Create a free Notion account.
- Login to Notion and go to the workspace you want your intergration to be. Then,
in the left tab go to Settings & Members > Workspace > Integrations. - Press the Develop your own integrations option, which will redirect you to a new page.
- Press the New integration button. Then:
- Give it a name and upload a logo if desired.
- Associate the integration to your desired Notion workspace.
- Check all Content Capabilities (Read content, Update content and
Insert content). - Set the User Capabilities to any option.
- Afterwards, press the Submit button.
- Access your to your created integration site and generate or copy the token from
Internal Integration Token. Then paste it in theNOTION_TOKEN
inside
secrets.py
. (Simply create a copy of thesecrets_template.py
file)
To correctly access, append or modify data in a Notion page, you first have to share it
with the created integration. To do so:
- Go to the page you want to add information and press Share on the right top corner.
- Press the Invite button and select your integration.
- Then press the 3 horizontal dots ··· on the right top corner. Select Copy link
and paste it somewhere. Then just extract the latest characters from the latest-
separator. Finally, use that as thepage_id
for the Notion page to add information to.- Example:
https://www.notion.so/3-Aproximaci-n-2-ff80bb15e4f24a7daa729da24bf8177b Start of page ID {*******************************}
Currently the blocks.py
provide Python dictionaries for the Notion blocks
included in the table below. For further information, you can visit the official
Notion API block reference using the links in the table. If you want to know
how to create this blocks in Python, open the notion.py
file for examples.
Notion block | Support |
---|---|
paragraph | ✔️ |
heading 1 | ✔️ |
heading 2 | ✔️ |
heading 3 | ✔️ |
callout | ✔️ |
quote | ✔️ |
bulleted_list_item | ✔️ |
numbered_list_item | ✔️ |
to_do | ✔️ |
toggle | ✔️ |
code | ✔️ |
child_page | ❌ |
child_database | ❌ |
embed | ✔️ |
image | ✔️ |
video | ✔️ |
file | ✔️ |
✔️ | |
bookmark | ✔️ |
equation | ✔️ |
divider | ✔️ |
table_of_contents | ✔️ |
breadcrumb | ✔️ |
columns | ❌ |
link_preview | ❓ |
template | ❌ |
link_to_page | ❌ |
synced_block | ❌ |
table | ❌ |
table_row | ❌ |
(All these functionalities can be found inside blocks.py
)
- Print with indents a dictionary Notion block for readability purposes using
theprint_block()
function. - Adds children object to dictionary containing a Notion block using the
add_children_to_block()
function. - Make a children Notion block to be appended to anything (like a page) using
theappend_blocks()
function.
It is necessary to use the markdown_to_notion()
method with strings containing markdown
delimiters.
Delimiter | Format description |
---|---|
** | Bold text |
_ | Italic text |
~ | |
` | Inline code |
# Right way to create formatted text
notion_blocks = markdown_to_notion("**This is bolded text** and this is not")
# Wrong ways to create formatted text
notion_blocks = markdown_to_notion("# This is a heading")
notion_blocks = markdown_to_notion("#This is a heading #")
For the heading (#
, ##
, ###
) and to-do ([]
) blocks, only add delimiter at the
begining.
To separate blocks, use the \n
delimiter.
# Right way to create heading block
notion_blocks = markdown_to_notion("#This is a heading\n[]This is a to-do block")
# Wrong ways to create heading block
notion_blocks = markdown_to_notion("# This is a heading\n []This is a to-do block")
notion_blocks = markdown_to_notion("#This is a heading #\n []This is a to-do block[]")
Install the python package for the Dropbox API by running:
pip install dropbox
- or - pythonX.Y -m pip install dropbox
(where X.Y
correspond to the Python version you want to install the package in).
To interact with Dropbox, you first need to create an app inside. To do so:
- Create a free Dropbox account (with 2Gb os storage) or a paid account.
- Go to the developer website page and press Create app. Then:
- Choose an API: Select Scoped access.
- Choose the type of access you need: Select App folder (recommended) or
Full Dropbox. - Name your app: Provide a name for the application.
- Once the developer app is ready, access its page and configure the Permissions tab.
- It is recommended to check all
*.write
permissions from Account Info,
Files and folders and Collaboration sections.
- It is recommended to check all
- Go to the Settings tab and generate an OAuth 2 token.
- It is recommended to set the Access token expiration to No expiration.
- Copy and secure the generated token. Then paste it in the
DROPBOX_TOKEN
inside
secrets.py
. (Simply create a copy of thesecrets_template.py
file)
With the dropbox_sdk.py
file you can create an instance of DropboxClient
to:
- View all files in a Dropbox directory using the
view_files()
method. - Upload all files included in a local path using the
upload_all_files()
method.
Additionally, this function returns a dictionary with the name of each file
uploaded with its corresponding raw share link from Dropbox (to be used for
Notion blocks).