Automatic Google Docs Archiving
Automatic Google Docs Archiving

Automatic Google Docs Archiving

Tags
Google DocsPHPAPI

There are some Google Docs that get regularly updated that I wanted to snapshot to have an archive of past revisions… so I set up an automation to periodically fetch the Google Docs and archive them to a folder. (In this case, Cronicle is the job automation scheduler I used to run this snapshot at regular intervals).

image

Google has an undocumented endpoint that will deliver a Google doc in one of the many formats you can use to save a document to your computer (typically, using the File > Download menu). Format types include html, pdf, zip, etc.

`https://docs.google.com/document/d/${documentId}/export?format=${format}`

Using this url, you can fetch the contents of any Google Doc and save the response to a file locally on your machine. Here’s an example written in Python:

#!/usr/bin/env python3

import urllib.request

def download_google_doc(file_name, doc_id, file_format="pdf"):
	urllib.request.urlretrieve(f"https://docs.google.com/document/d/{doc_id}/export?format={file_format}", file_name)
	
download_google_doc("myDoc.html", "documentIdFromUrlHere", "html")

More Formatting Options Can Be Found Here