iT邦幫忙

0

使用Python的 Notion Client函式庫於頁面中新增元件

  • 分享至 

  • xImage
  •  

步驟

  1. 安裝python的 notion-client 函式庫by pip install notion-client ,可參考官方文件的說明其中也有使用教學
  2. 上一篇文章的說明使用notion-client的pages.create()方法把資料庫設為parent來建立新頁面,或是想成在資料庫中建立新頁面即可
  3. 把新頁面id記錄下,使用 notion.blocks.children.append(block_id = 新頁面id ,children) 在該頁面中append元件

範例

範例中pages.create()的方法及 your_notion_secret & your_database_id 在上一篇文章中有說明,其餘的code把子元件的type設定為嵌入連結 -> url設定為google首頁 -> 把子元件append到新頁面:

from notion_client import Client

# Initialize Notion client with your integration token
notion = Client(auth="your_notion_secret")

# Create a new page
new_page = notion.pages.create(
    parent={"database_id": "your_database_id"},
    properties={
        "title": {
            "title": [
                {
                    "text": {
                        "content": "Your Page Title"
                    }
                }
            ]
        }
    }
)
page_id = new_page['id']  #儲存新頁面的id

# Append a child block to the new page
notion.blocks.children.append(
    block_id=page_id,
    children=[  #要append在新頁面中的元件
        {
            "object": "block",
            "type": "embed",
            "embed": {
                "url": "https://www.google.com"
            }
        }
    ]
)

print(f"Page created with ID: {page_id}")

圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言