-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_utils.py
48 lines (41 loc) · 1.64 KB
/
upload_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from googleapiclient.discovery import build
from google.oauth2.service_account import Credentials
import io
import os
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
from googleapiclient.discovery import build
def insert_image_to_sheet(spreadsheet_id, sheet_id, image_id, start_col, start_row,json_file='pypong.json'):
creds = Credentials.from_service_account_file(json_file)
sheet_service = build('sheets', 'v4', credentials=creds)
requests = [{
"insertImage": {
"location": {
"sheetId": sheet_id,
"overlayPosition": {
"anchorCell": {
"rowIndex": start_row,
"columnIndex": start_col
},
"offsetXPixels": 0,
"offsetYPixels": 0
},
},
"imageId": image_id,
}
}]
body = {
'requests': requests
}
response = sheet_service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body).execute()
return response
def upload_image_and_delete_from_drive(filename,json_file='pypong.json'):
creds = Credentials.from_service_account_file(json_file)
drive_service = build('drive', 'v3', credentials=creds)
# Upload the file
file_metadata = {'name': filename}
media = MediaFileUpload(filename, mimetype='image/png')
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
file_id = file.get('id')
# Delete the file after obtaining the file ID
#drive_service.files().delete(fileId=file_id).execute()
return file_id