Automated Documents — Google Docs API

This article is published during COVID-19 outbreak times

Do you create tons of invoices/bills/statements for your business? Do you need to capture your IoT (Internet Of Things) sensors data into a spreadsheet? Do you want to automate data capture and collaborate with teams? Do you want to scrap WIKIPEDIA or similar web content in a document? etc …

If such thoughts come to your mind related for your project or businesses? Its time to automate and focus on core competency of your business. Check Google G-suite developers APIs. Google has developer APIs for variety of products but this article focuses on Google Docs APIs.

Its quite simple and specially with Python and this article tries to bring you to further speed.

Google documents are kept in Google Drive and hence you would need access to services of ‘Google-Drive’ and ‘Google-Docs’ for this exercise. This also means, whatever you can do on your Google Drive and Document UI, you can do the same with APIs.


  1. A Google account.
  2. Please go though the document structure. this one pager is excellent.


Step 1:

Step 2:

pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

Step 3: (Optional)

pip3 install wikipedia-api

Code Walk Through

For this example, i have given following two scopes:,

These scopes give full access to the drive as well as to documents to the application, but please be very careful when you enable such access and never share following two files.

1. credentials.json
2. token.pickle

As you can see, i have also not shared these two files in the project, you need to create credentials.json for your project. token.pickle is generated after first run to avoid authentication for any further subsequent application runs.

The template i used in this example can be found here. You can add your own template(s), please make sure the code is in sync with with the templates. In the present code i have just considered a single template.

In the code, after selecting the template, make a duplicate copy and editing it. We will be performing following operations:

  1. Replace a tag in the document tag with a value.
  2. Search for a text and get its start and end indexes.
  3. Insert text at a specific location.
  4. Insert an image in the document.
  5. Insert a hyperlink in the document.
  6. Formatting text at a specific location.

Complete code can be found on my github repository.

Following command to generate the document and the generated document looks like following this.


Hope this helps in getting started with docs API. Next step would be to build application with spreadsheets and integrating them together.

Co-founder Logits Systems Biometric on the edge.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store