iT邦幫忙

0

使用Python Notion API匯入新頁面

  • 分享至 

  • xImage
  •  

本文紀錄個人認為Notion API使用上不夠直觀的地方,有任何不夠正確的部分還請見諒及留言教學相長。

前置步驟

  1. 取得API key:
    前往 https://www.notion.so/profile/integrations ,如果沒有可以建立一個integrationhttps://ithelp.ithome.com.tw/upload/images/20250212/20171845ydProvoEn5.png
    填欄位只要注意關聯工作空間,其他後來都能夠修改,但是關聯工作空間如果選錯就要再重建一個integration了https://ithelp.ithome.com.tw/upload/images/20250212/20171845lntk9Y23l8.png
    儲存後會有個密鑰,使用Python的notion_client會需要用到,另外可以照需求順便設定內容、評論和使用者功能的權限。
  2. 連結頁面
    建立完integration後,去頁面連結剛剛新增的API key,https://ithelp.ithome.com.tw/upload/images/20250213/20171845nSTZD9oBgL.png

使用Python呼叫API建立新頁面

把剛剛API key的secret用來連結notion_client,以及想要新增在哪個頁面中,也就是修改page_id為網址 www.notion.so/ 後面的那一串

from notion_client import Client

notion = Client(auth="your_notion_secret")

# 建立頁面的函式
def create_notion_page(page_title):
    properties = {
        "title": [
            {
                "text": {
                    "content": page_title
                }
            }
        ]
    }

    parent = {"type": "page_id", "page_id": "your_page_id"}
    new_page = notion.pages.create(parent=parent, properties={"title": properties})
    
    page_id = new_page['id']
    print(f"成功建立頁面 {page_title},Page ID: {page_id}")

print("全部操作完成!請檢查您的 Notion。")

總結

Notion幾乎可以用API進行所有行為: 內容、評論和使用者功能,也可以連結很多應用,版上也有文章可以參考。

參考連結

https://ithelp.ithome.com.tw/articles/10303039
https://developers.notion.com/docs/getting-started


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言