Convert Markdown to Google Docs API requests
Project description
gravitas-md2gdocs
Convert Markdown to Google Docs API requests.
Installation
pip install gravitas-md2gdocs
To use the Google API helper functions:
pip install gravitas-md2gdocs[google]
Usage
Basic: Get API Requests
from gravitas_md2gdocs import to_requests
markdown = """
# My Document
This is **bold** and *italic* text.
- Bullet one
- Bullet two
> A blockquote
"""
requests = to_requests(markdown)
# Use with your own Google Docs API client
docs_service.documents().batchUpdate(
documentId=doc_id,
body={'requests': requests}
).execute()
Insert at a Specific Position
requests = to_requests(markdown, start_index=50)
Helper Functions (requires gravitas-md2gdocs[google])
from gravitas_md2gdocs import append, replace, insert_at, replace_range
# Append to end of document
append(docs_service, doc_id, "## New Section\n\nMore content")
# Replace entire document
replace(docs_service, doc_id, new_markdown)
# Insert at specific position
insert_at(docs_service, doc_id, "**inserted**", index=25)
# Replace a range (characters 10-50)
replace_range(docs_service, doc_id, "**replacement**", start=10, end=50)
Supported Markdown
| Syntax | Example |
|---|---|
| Headers | # H1 through ###### H6 |
| Bold | **bold** or __bold__ |
| Italic | *italic* or _italic_ |
| Bold + Italic | ***both*** |
| Strikethrough | ~~struck~~ |
| Inline code | `code` |
| Code blocks | ```code``` |
| Links | [text](url) |
| Bullet lists | - item or * item |
| Numbered lists | 1. item |
| Blockquotes | > quote |
Setting Up Google Docs API
- Create a project in Google Cloud Console
- Enable the Google Docs API
- Create credentials (OAuth 2.0 or Service Account)
- Install the client library:
pip install google-api-python-client google-auth
Example setup:
from google.oauth2 import service_account
from googleapiclient.discovery import build
credentials = service_account.Credentials.from_service_account_file(
'service-account.json',
scopes=['https://www.googleapis.com/auth/documents']
)
docs_service = build('docs', 'v1', credentials=credentials)
# Create a new doc
doc = docs_service.documents().create(body={'title': 'My Doc'}).execute()
doc_id = doc['documentId']
# Add content
from gravitas_md2gdocs import to_requests
requests = to_requests("# Hello World\n\nThis is **markdown**!")
docs_service.documents().batchUpdate(
documentId=doc_id,
body={'requests': requests}
).execute()
Alternative: Google Drive API with Native Markdown
As of July 2024, Google Drive API supports uploading markdown directly:
from googleapiclient.http import MediaInMemoryUpload
media = MediaInMemoryUpload(
markdown_text.encode('utf-8'),
mimetype='text/markdown'
)
file = drive_service.files().create(
body={
'name': 'My Document',
'mimeType': 'application/vnd.google-apps.document'
},
media_body=media
).execute()
Use gravitas-md2gdocs when you need to:
- Append/insert into existing documents
- Replace specific sections
- Have fine-grained control over formatting
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gravitas_md2gdocs-0.1.0.tar.gz.
File metadata
- Download URL: gravitas_md2gdocs-0.1.0.tar.gz
- Upload date:
- Size: 48.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb3122fe9fa35c528f3f00b785d3f1398d350082d5d03f60f56c895bdcc68033
|
|
| MD5 |
cd10fad354ea2f3f200becd6c976ade8
|
|
| BLAKE2b-256 |
7e6c3c51b1deda0ee0dc83b3f5f37274434eb05aa2fa559906ac6c157de003e4
|
Provenance
The following attestation bundles were made for gravitas_md2gdocs-0.1.0.tar.gz:
Publisher:
ci.yml on Significant-Gravitas/gravitas-md2gdocs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gravitas_md2gdocs-0.1.0.tar.gz -
Subject digest:
bb3122fe9fa35c528f3f00b785d3f1398d350082d5d03f60f56c895bdcc68033 - Sigstore transparency entry: 757892612
- Sigstore integration time:
-
Permalink:
Significant-Gravitas/gravitas-md2gdocs@152f90a5b4b702bd2831105ccffbbd07fd147642 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Significant-Gravitas
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@152f90a5b4b702bd2831105ccffbbd07fd147642 -
Trigger Event:
release
-
Statement type:
File details
Details for the file gravitas_md2gdocs-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gravitas_md2gdocs-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cb0627779fdd65c1604818af4142eea1b25d055060183363de1bae4d9e46508
|
|
| MD5 |
4d6b2ac04af833aadcfb8c2c1f42814d
|
|
| BLAKE2b-256 |
2aadde3cb7a498c8b5ab69074741c1083d7b7f0d20edb1f437ea0c7dfb851668
|
Provenance
The following attestation bundles were made for gravitas_md2gdocs-0.1.0-py3-none-any.whl:
Publisher:
ci.yml on Significant-Gravitas/gravitas-md2gdocs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gravitas_md2gdocs-0.1.0-py3-none-any.whl -
Subject digest:
0cb0627779fdd65c1604818af4142eea1b25d055060183363de1bae4d9e46508 - Sigstore transparency entry: 757892616
- Sigstore integration time:
-
Permalink:
Significant-Gravitas/gravitas-md2gdocs@152f90a5b4b702bd2831105ccffbbd07fd147642 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Significant-Gravitas
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@152f90a5b4b702bd2831105ccffbbd07fd147642 -
Trigger Event:
release
-
Statement type: