コンテンツにスキップ

ページを作る

ipynbFile notionapi_02__ページを作る.ipynb

In [1]:

1
2
3
4
5
6
from notion_client import Client

with open("./token.txt",'r') as f:
    TOKEN = f.readline()

notion = Client(auth=TOKEN)

In [2]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
pages = notion.pages.create(**{
    "parent": {"database_id": "d6f7115afc4549a092dede3a56a24f90"},
    "properties": {
        "Date":{
            "date":{
                "start":"2022-01-14"
            }
        }
    }
})

In [5]:

1
ID = pages['id'].replace('-','')

In [10]:

1
blocks = notion.blocks.children.list(block_id=ID)

In [11]:

1
print(blocks)

Success

1
{'object': 'list', 'results': [{'object': 'block', 'id': '15b381ba-2673-452b-8750-50ebf1672193', 'created_time': '2022-01-13T16:41:00.000Z', 'last_edited_time': '2022-01-13T16:41:00.000Z', 'has_children': False, 'archived': False, 'type': 'paragraph', 'paragraph': {'text': [{'type': 'text', 'text': {'content': 'aaa', 'link': None}, 'annotations': {'bold': False, 'italic': False, 'strikethrough': False, 'underline': False, 'code': False, 'color': 'default'}, 'plain_text': 'aaa', 'href': None}]}}, {'object': 'block', 'id': '3047823b-4a6c-476a-9b31-d3ad1afcb16e', 'created_time': '2022-01-13T16:41:00.000Z', 'last_edited_time': '2022-01-13T16:41:00.000Z', 'has_children': False, 'archived': False, 'type': 'paragraph', 'paragraph': {'text': []}}], 'next_cursor': None, 'has_more': False}

In [16]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
blocks: list = [
     {
         "object": "block",
         "type": "heading_2",
         "heading_2": {
                 "text": [
                     {"type": "text", "text": {"content": "H2_TITLE"}}
                 ]
         }
     }
]

In [17]:

1
notion.blocks.children.append(block_id=ID,children=blocks)

Success

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
{'object': 'list',
 'results': [{'object': 'block',
   'id': 'd76a8431-582c-4835-b5eb-53728c360326',
   'created_time': '2022-01-13T16:45:00.000Z',
   'last_edited_time': '2022-01-13T16:45:00.000Z',
   'has_children': False,
   'archived': False,
   'type': 'heading_2',
   'heading_2': {'text': [{'type': 'text',
      'text': {'content': 'H2_TITLE', 'link': None},
      'annotations': {'bold': False,
       'italic': False,
       'strikethrough': False,
       'underline': False,
       'code': False,
       'color': 'default'},
      'plain_text': 'H2_TITLE',
      'href': None}]}}],
 'next_cursor': None,
 'has_more': False}