Skip to content

Instantly share code, notes, and snippets.

@clement-moulin-frier
Last active October 2, 2024 15:26
Show Gist options
  • Save clement-moulin-frier/cddb5ab6e606071c684d92f4887ee68f to your computer and use it in GitHub Desktop.
Save clement-moulin-frier/cddb5ab6e606071c684d92f4887ee68f to your computer and use it in GitHub Desktop.
Given the path of a google document stored locally (with extension .gdoc, .gsheets ...), the script will open it in the default web browser. Useful if you stop using Google Drive as your cloud service, but still want to be able to open google documents from your file explorer.
import sys
import json
import webbrowser
from pathlib import Path
def open_google_doc(file_path):
# Read the file
with open(file_path, 'r') as file:
data = json.load(file)
# Extract the doc_id
doc_id = data.get('doc_id')
if not doc_id:
print("Error: 'doc_id' not found in the file.")
return
# Determine the correct URL based on the file extension
file_extension = Path(file_path).suffix.lower()
url_templates = {
'.gdoc': 'https://docs.google.com/document/d/{}',
'.gsheet': 'https://docs.google.com/spreadsheets/d/{}',
'.gslides': 'https://docs.google.com/presentation/d/{}',
'.gform': 'https://docs.google.com/forms/d/{}',
'.gdraw': 'https://docs.google.com/drawings/d/{}',
'.gmaps': 'https://www.google.com/maps/d/{}',
'.gsite': 'https://sites.google.com/view/{}',
'.gjam': 'https://jamboard.google.com/d/{}'
}
url_template = url_templates.get(file_extension, 'https://docs.google.com/open?id={}')
# Construct the URL
url = url_template.format(doc_id)
# Open the URL in the default browser
webbrowser.open(url)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <path_to_google_doc_file>")
else:
file_path = sys.argv[1]
open_google_doc(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment