-
Notifications
You must be signed in to change notification settings - Fork 2
/
google-docs-example.py
executable file
·306 lines (248 loc) · 9.44 KB
/
google-docs-example.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# Written by Milind Deore <[email protected]>, March-2020
from __future__ import print_function
import pickle
import os.path
import sys
import random
import wikipediaapi
import uuid
from datetime import date
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
DEBUG = 0
if DEBUG:
import json
class Covid19Docs:
def __init__(self):
# If modifying these scopes, delete the file token.pickle.
self.DRIVE_SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/documents']
self.creds = None
# Add all your created templates in the below list.
self.templates = ['1a0C3AQ290uc0yFcVhLD36sj12mQdmv_VnSjh4WhnNvQ']
self.drive_service = None
self.docs_service = None
self.wiki = wikipediaapi.Wikipedia(language='en', extract_format=wikipediaapi.ExtractFormat.WIKI)
self.covid19_wiki = self.wiki.page("Coronavirus_disease_2019")
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
self.creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not self.creds or not self.creds.valid:
if self.creds and self.creds.expired and self.creds.refresh_token:
self.creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', self.DRIVE_SCOPES)
self.creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(self.creds, token)
if self.creds == None:
print('ERROR : Service credentials unavailable!')
sys.exit()
# Start drive and docs services.
self.drive_service = build('drive', 'v3', credentials=self.creds)
self.docs_service = build('docs', 'v1', credentials=self.creds)
def pick_a_template(self):
"""
There can be a list of templated that you can pick from. Make sure you add them
to template list.
"""
title = 'covid-19_' + str(uuid.uuid1())
body = {
'name': title,
'description': 'Covid-19 Wiki'
}
drive_response = self.drive_service.files().copy(
fileId=random.choice(self.templates), body=body).execute()
document_copy_id = drive_response.get('id')
print('Dup copy ID : {0}'.format(document_copy_id))
return document_copy_id
def replace_a_data(self, key, value):
"""
Replace text in the document using 'key/tag'
"""
dic = {'replaceAllText': {
'containsText': {
'text': '{{' + key + '}}',
'matchCase': 'true'
},
'replaceText': value,
}}
return dic
def insert_bullet_text(self, idx, title):
"""
Create bullets in the document.
"""
requests = [
{
'insertText': {
'location': {
'index': idx,
},
'text': title
}},{
'createParagraphBullets': {
'range': {
'startIndex': idx,
'endIndex': idx + len(title)
},
'bulletPreset': 'BULLET_DIAMONDX_ARROW3D_SQUARE',
}
}]
result = self.docs_service.documents().batchUpdate(
documentId=doc_id, body={'requests': requests}).execute()
if result:
pass
return len(title)
def insert_text(self, idx, text):
"""
Insert raw text without formating.
"""
req = {'insertText': {
'location': {
'index': idx,
},
'text': text
}}
return req, len(text)
def format_text(self, starti, endi, is_bold, is_italic, is_underline):
"""
Format the text as following:
- Bold
- Italic
"""
req = {'updateTextStyle': {
'range': {
'startIndex': starti,
'endIndex': endi
},
'textStyle': {
'bold': is_bold,
'italic': is_italic,
'underline': is_underline
},
'fields': 'bold, italic'
}}
return req
def hwd_batch_update(self, doc_id, requests):
"""
Batch update the document with the requests.
"""
result = self.docs_service.documents().batchUpdate(
documentId=doc_id, body={'requests': requests}).execute()
return result
def hwd_insert_hyperlink(self, doc_id, start_idx, end_idx, url):
"""
Insert hyperlink to the text for a given range in the
document body.
"""
requests = [
{
"updateTextStyle": {
"textStyle": {
"link": {
"url": url
}
},
"range": {
"startIndex": start_idx,
"endIndex": end_idx
},
"fields": "link"
}}
]
result = self.docs_service.documents().batchUpdate(
documentId=doc_id, body={'requests': requests}).execute()
return result
def get_text_range_idx(self, doc_id, match_text):
"""
Find text and their start and end index.
"""
# Do a document "get" request and print the results as formatted JSON
result = self.docs_service.documents().get(documentId=doc_id).execute()
if DEBUG:
print('RX Data {0}'.format(json.dumps(result, indent=4)))
with open('data.json', 'w') as f:
json.dump(result, f, indent=4)
data = result.get('body').get('content')
startIdx = 0
endIdx = 0
for d in data:
para = d.get('paragraph')
if para is None:
continue
else:
elements = para.get('elements')
for e in elements:
if e.get('textRun'):
content = e.get('textRun').get('content')
print(' {}'.format(content))
if match_text in content:
print('matched')
startIdx = e.get('startIndex')
endIdx = e.get('endIndex')
return startIdx, endIdx
def insert_image(self, start_idx, url, h, w):
"""
Insert PNG, JPEG, GIF images inline, while adding text.
"""
request = [{
'insertInlineImage': {
'location': {
'index': start_idx
},
'uri':
url,
'objectSize': {
'height': {
'magnitude': h,
'unit': 'PT'
},
'width': {
'magnitude': w,
'unit': 'PT'
}
}
}
}]
return request
def print_main_sections(self, doc_id, endi):
section_titles = ['Signs and symptoms', 'Cause', 'Diagnosis', 'Prevention']
requests = []
wr_idx = endi + 1
for s in self.covid19_wiki.sections:
if s.title in section_titles:
section_title = '\n' + s.title + ' :\n'
req, idx = self.insert_text(wr_idx, section_title)
requests.append(req)
req = self.format_text(wr_idx, wr_idx + idx, True, False, False)
requests.append(req)
wr_idx += idx
req, idx = self.insert_text(wr_idx, s.text + '\n')
requests.append(req)
wr_idx += idx
if s.title == 'Prevention':
req = self.insert_image(wr_idx, 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Covid-19-curves-graphic-social-v3.gif/640px-Covid-19-curves-graphic-social-v3.gif', 480, 276)
requests.append(req)
req = self.insert_image(wr_idx, 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Covid-19-curves-graphic2-stopthespread-v3.gif/640px-Covid-19-curves-graphic2-stopthespread-v3.gif', 480, 276)
requests.append(req)
result = self.docs_service.documents().batchUpdate(
documentId=doc_id, body={'requests': requests}).execute()
return result, endi
if __name__ == '__main__':
c_docs = Covid19Docs()
doc_id = c_docs.pick_a_template()
requests = []
requests.append(c_docs.replace_a_data('title', 'Coronavirus Disease 2019'))
today = date.today()
requests.append(c_docs.replace_a_data('date', '{0}'.format(today)))
c_docs.hwd_batch_update(doc_id, requests)
starti, endi = c_docs.get_text_range_idx(doc_id, 'Details :')
rlt, endi = c_docs.print_main_sections(doc_id, endi)