/notion-to-html

Notion to HTML tags w/ python

Primary LanguagePython

Notion To HTML

Transforms Notion page into HTML using official Notion API

Concepts and Notes

  • While in transforming the notion block to its corresponding HTML tag, our contents team had some needs to add customizing rules for better SEO. This library accepts the overriding converters.
  • Notion API has request limits such as Rate Limit and Size Limit. This library does not take care of them now. Just run it slowly
  • The block parsers and also the converters are not fully supported. Pull requests are always welcomed!

Usage

Quickstart

  1. Set environment variables

    • CONVERTED_CDN_PREFIX : Notion files are temporarily available right after fetching them, and they have to be served by your own cdn. Tags such as <img> src will be set using this variable.
    • NOTION_TOKEN : To authorize the access to Notion API, the token is needed. See this page to get one.
  2. See example.py and write codes for you.

Examples

# you can see the example in example.py
from notion.parse import NotionParser
from notion.builder import HtmlBuilder
from notion import converter

class CustomHeading1Converter(converter.Heading1Converter):
    tag_type: str = "h2"
    
class CustomHeading2Converter(converter.Heading2Converter):
    tag_type: str = "h3"


notion_parser = NotionParser()
html_builder = HtmlBuilder()
html_builder.override_converter_classes(
   [
       CustomHeading1Converter,
       CustomHeading2Converter,
   ]
)

page_id = 'some-page-id'

blocks = notion_parser.parse_page(page_id)
html_builder.build(blocks)

html_str = str(html_builder.soup)
files_to_download = html_builder.files_to_download

# process html files if needed
html_str = html_str.replace("</p><p>", "<br>")

# upload or write html
save_html(html_str)

# download and files
save_files(files_to_download)

Dependencies

  • requests
  • beautifulsoup4

Related Projects

  • notion-py : Unofficial Python 3 client for Notion.so API v3.

TODO

  • Add pytest and test cases (not fully tested yet)
  • Make this as python library or pip available