0

I try to sync notion database with google sheet by Visual studio code with this script

1  from __future__ import print_function
2  import pickle
3  import os.path
4  from googleapiclient.discovery import build
5  from google_auth_oauthlib.flow import InstalledAppFlow
6  from google.auth.transport.requests import Request
7
8  from notionpy.notion.client import NotionClient
9
10 SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
11
12 # start - CHANGE THESE
13 SAMPLE_SPREADSHEET_ID = '<My_Spreadsheet_ID>'
14 SAMPLE_RANGE_NAME = '<Test!A1:AZ>'
15 NOTION_V2_TOKEN = "<Token ID>" 
16 NOTION_PAGE_LINK = "<https://www.notion.so/xxxxxxxxxxx>"
17 # end   - CHANGE THESE
18
19 def get_sheet():
20  """Shows basic usage of the Sheets API.
21  Prints values from a sample spreadsheet.
22  """
23  creds = None
24  if os.path.exists('token.pickle'):
25      with open('token.pickle', 'rb') as token:
26          creds = pickle.load(token)
27  if not creds or not creds.valid:
28      if creds and creds.expired and creds.refresh_token:
29          creds.refresh(Request())
30      else:
31          flow = InstalledAppFlow.from_client_secrets_file(
32              'credentials.json', SCOPES)
33          creds = flow.run_local_server(port=0)
34      with open('token.pickle', 'wb') as token:
35          pickle.dump(creds, token)
36
37  service = build('sheets', 'v4', credentials=creds)
38
39  sheet = service.spreadsheets()
40  result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
41                              range=SAMPLE_RANGE_NAME).execute()
42  values = result.get('values', [])
43
44  if not values:
45      print('No data found.')
46      return None
47  else:
48      print('%s rows found.' % len(values))
49      return values
50
51 def get_prop_type(data):
52  # todo parse data to support types
53
54  return 'text'
55
56 def notion_update(data):
57  client = NotionClient(token_v2=NOTION_V2_TOKEN)
58    
59  block = client.get_block(NOTION_PAGE_LINK)
60  cv = block.collection
61  rows = cv.get_rows()
62
63  cv.clear_properties()
64  for row in rows:
65      row.remove()
66
67
68  title = data[0][0]
69  cv.set_property('title', title, slug=title, property_type='title')
70  for i, prop in enumerate(data[0][1:]):
71      prop_type = get_prop_type(data[1][1:][i]) if data[1] and data[1][1:] else 'text'
72      cv.set_property(prop, prop, slug=prop, property_type=prop_type)
73
74  for row in data[1:]:
75      new_row = cv.add_row()
76      for i, value in enumerate(row):
77          prop = data[0][i]
78          new_row.set_property(prop, value)
79
80 if __name__ == '__main__':
81  data = get_sheet()
82  notion_update(data)

I have already install

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib pip install notion

The error show

Exception has occurred: ModuleNotFoundError
No module named 'notionpy'
File "C:\Users\Documents\WORK\NotionAPI\sync.py", line 8, in
from notionpy.notion.client import NotionClient

0 Answers0