File tree 3 files changed +114
-0
lines changed
3 files changed +114
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Google Docs API Automation
2
+
3
+ This project demonstrates how to use the Google Docs API to automate reading and writing data in Google Docs Documents using Python.
4
+
5
+ ## Setup
6
+
7
+ ### Clone the Repository
8
+
9
+ ``` bash
10
+ git clone https://github.com/your-username/Google-Docs-API.git
11
+ ```
12
+
13
+ ### Navigate to the Project Directory
14
+
15
+ ``` bash
16
+ cd Google-Docs-API
17
+ ```
18
+
19
+ ### Install Required Libraries
20
+
21
+ ``` bash
22
+ pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
23
+ ```
24
+
25
+ ### Set Environment Variable
26
+ Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of your service account key:
27
+
28
+ ``` bash
29
+ set GOOGLE_APPLICATION_CREDENTIALS=path\t o\y our\s ervice-account-file.json
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Read Data from Google Docs
35
+ Run the Python script read.py to read data from your Google Docs:
36
+
37
+ ``` bash
38
+ python read.py
39
+ ```
40
+
41
+ ### Create Google Docs
42
+ Run the script to create Docs:
43
+
44
+ ``` bash
45
+ python create_doc.py
46
+ ```
Original file line number Diff line number Diff line change
1
+ from google .oauth2 import service_account
2
+ from googleapiclient .discovery import build
3
+
4
+ SCOPES = ['https://www.googleapis.com/auth/documents' ]
5
+ SERVICE_ACCOUNT_FILE = 'path/to/your/service-account-file.json'
6
+
7
+ credentials = service_account .Credentials .from_service_account_file (
8
+ SERVICE_ACCOUNT_FILE , scopes = SCOPES )
9
+ service = build ('docs' , 'v1' , credentials = credentials )
10
+
11
+ # Create a Document
12
+ document = service .documents ().create (body = {'title' : 'New Document' }).execute ()
13
+ document_id = document .get ('documentId' )
14
+ print (f'Document ID: { document_id } ' )
15
+
16
+ # Modifying the Document
17
+ requests = [
18
+ {
19
+ 'insertText' : {
20
+ 'location' : {
21
+ 'index' : 1 ,
22
+ },
23
+ 'text' : 'Hello, world!'
24
+ }
25
+ }
26
+ ]
27
+
28
+ result = service .documents ().batchUpdate (documentId = document_id , body = {'requests' : requests }).execute ()
29
+ print (document )
Original file line number Diff line number Diff line change
1
+ from google .oauth2 import service_account
2
+ from googleapiclient .discovery import build
3
+
4
+ SCOPES = ['https://www.googleapis.com/auth/documents' ]
5
+ SERVICE_ACCOUNT_FILE = 'path/to/your/service-account-file.json'
6
+
7
+ credentials = service_account .Credentials .from_service_account_file (
8
+ SERVICE_ACCOUNT_FILE , scopes = SCOPES )
9
+ service = build ('docs' , 'v1' , credentials = credentials )
10
+
11
+ document = service .documents ().create (body = {'title' : 'New Document' }).execute ()
12
+ document_id = document .get ('documentId' )
13
+
14
+ # Reading the Document
15
+ document = service .documents ().get (documentId = document_id ).execute ()
16
+ print (f'Title: { document .get ("title" )} ' )
17
+ print ('Content:' )
18
+ for element in document .get ('body' ).get ('content' ):
19
+ print (element .get ('paragraph' ).get ('elements' )[0 ].get ('textRun' ).get ('content' ))
20
+
21
+
22
+ # Formatting Text
23
+ requests = [
24
+ {
25
+ 'updateTextStyle' : {
26
+ 'range' : {
27
+ 'startIndex' : 1 ,
28
+ 'endIndex' : 13 ,
29
+ },
30
+ 'textStyle' : {
31
+ 'bold' : True ,
32
+ 'italic' : True ,
33
+ },
34
+ 'fields' : 'bold,italic'
35
+ }
36
+ }
37
+ ]
38
+
39
+ result = service .documents ().batchUpdate (documentId = document_id , body = {'requests' : requests }).execute ()
You can’t perform that action at this time.
0 commit comments