I am trying to read data from my google docs. So I am using python right now and I have implemented the Google Docs API and using python. I just copy pasted the code there provided by google and made some modifications and I successfully read the data LINE BY LINE but the TEXT ONLY! Now I am trying something new and have inserted an image. Here is what it looks like.
Very simple right... It has there a bulletpoint and sub bulletpoints containing an image and a "Hello" text. Now when I read the data (it reads it line by line) I tried printing out what the API returns and it returns a dictionary
containing dictionaries
again. Here is what it looks like.
{'startIndex': 1, 'endIndex': 41, 'paragraph': {'elements': [{'startIndex': 1, 'endIndex': 41, 'textRun': {'content': 'This is the Python Programming Language\n', 'textStyle': {}}}], 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT', 'direction': 'LEFT_TO_RIGHT', 'indentFirstLine': {'magnitude': 18, 'unit': 'PT'}, 'indentStart': {'magnitude': 36, 'unit': 'PT'}}, 'bullet': {'listId': 'kix.y7w314ij0ywy', 'textStyle': {'underline': False}}}}
{'startIndex': 41, 'endIndex': 43, 'paragraph': {'elements': [{'startIndex': 41, 'endIndex': 42, 'inlineObjectElement': {'inlineObjectId': 'kix.o4cuh6wash2n', 'textStyle': {}}}, {'startIndex': 42, 'endIndex': 43, 'textRun': {'content': '\n', 'textStyle': {}}}], 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT', 'direction': 'LEFT_TO_RIGHT', 'indentFirstLine': {'magnitude': 54, 'unit': 'PT'}, 'indentStart': {'magnitude': 72, 'unit': 'PT'}}, 'bullet': {'listId': 'kix.y7w314ij0ywy', 'nestingLevel': 1, 'textStyle': {'underline': False}}}}
{'startIndex': 43, 'endIndex': 49, 'paragraph': {'elements': [{'startIndex': 43, 'endIndex': 49, 'textRun': {'content': 'Hello\n', 'textStyle': {}}}], 'paragraphStyle': {'namedStyleType': 'NORMAL_TEXT', 'direction': 'LEFT_TO_RIGHT', 'indentFirstLine': {'magnitude': 54, 'unit': 'PT'}, 'indentStart': {'magnitude': 72, 'unit': 'PT'}}, 'bullet': {'listId': 'kix.y7w314ij0ywy', 'nestingLevel': 1, 'textStyle': {'underline': False}}}}
As you can see there , there are 3 dictionaries containing their key
and value
pairs. Take note that those three are for every line from the document. As you can also observe there is the key content
and its value
(s) are the text from the document.
If you look at the nested dictionaries it is these ones:
{'content': 'This is the Python Programming Language\n', 'textStyle': {}}
{'content': '\n', 'textStyle': {}}
{'content': 'Hello\n', 'textStyle': {}}
Now what I've noticed is it returned a \n
for the line where the image contains. Also I've looked for at least it could have probably have a key
and its value would be a temporary url for the image however it doesn't seem to have that. So my question is there a way to somehow read this image (also EXTRACT IT) using this API that I am using? Probably I am just missing something out... Can someone help me with this? Any other alternative solution would be very much appreciated! Thank you!
By the way here is the source code provided by google and I have made modifications on the read_strucutural_elements
function on how it would read the data for my personal purpose but there as you can see that's how it works where the API returns a dictionary for every line data. I've also noticed that the API somehow really does read it line by line and returns a dictionary
of it
def main():
"""Shows basic usage of the Docs API.
Prints the title of a sample document.
"""
creds = None
# 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:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('docs', 'v1', credentials=creds)
# Retrieve the documents contents from the Docs service.
document = service.documents().get(documentId=DOCUMENT_ID).execute()
#print('The title of the document is: {}'.format(document.get('title')))
data = read_strucutural_elements(document.get("body").get("content"))
Here is the read_strucutural_elements
function and I just print out there the elements from the elements
parameter, where that parameter contains those data line by line.
def read_strucutural_elements(elements):
for value in elements:
print(value) #the value of the value variable is the nested dictionaries I've shown above
print()
Thank you very much!
looking at the dictionary output, the image is a inlineObject with a specific id. you should be able to retrieve the image using its url. to get the url, see related question: How to get the url to Google doc image