This is a sample script for creating a table to Google Document by retrieving values from Google Spreadsheet for Python.
Before you use this script, please install python library of gdoctableapppy.
$ pip install gdoctableapppy
Sample script:
This sample script uses Service Account.
In this sample script, the values are retrieved from Sheet1!A1:C5
of Spreadsheet, and new table is created to the Document using the values.
from google.oauth2 import service_account
from gdoctableapppy import gdoctableapp
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'credential.json' # Please set the json file of Service account.
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=creds)
spreadsheet_id = '###' # Please set here
document_id = '###' # Please set here
res = service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id, range='Sheet1!A1:C5').execute()
values = res['values']
resource = {
"service_account": creds,
"documentId": document_id,
"rows": len(values),
"columns": len(values[0]),
"append": True,
"values": values
}
res = gdoctableapp.CreateTable(resource)
print(res)