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.
Python3.7(2.7 support is going and the world is moving on too.) along with
pipto install python packages.
- A Google account.
- Please go though the document structure. this one pager is excellent.
Very well documented steps can be found here. Please enable the APIs so that you can start using cloud services.
Follow the above links to enable access to Google documents APIs. Download the
credentials.json to your work area where you will be creating your google client project.
Install Google’s python client for cloud APIs.
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Step 3: (Optional)
We are scrapping content from Wikipedia and hence we need this python package, otherwise its optional.
pip3 install wikipedia-api
Code Walk Through
As we discussed before, documents are kept in the drive and hence Authentication and Access control needs to be controlled via Drive APIs. During user authentication we also need to give the access rights and this is defined in the following link ‘Scope’:
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.
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:
- Replace a tag in the document tag with a value.
- Search for a text and get its start and end indexes.
- Insert text at a specific location.
- Insert an image in the document.
- Insert a hyperlink in the document.
- Formatting text at a specific location.
Complete code can be found on my github repository.
Using google docs APIs, populate Wikipedia content. - milinddeore/google-docs-example
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.