Automating Google Docs, Sheets, and Slides with Python
Google Docs, Sheets, and Slides are widely used for collaboration and document automation. Python provides APIs to create, edit, and manipulate these files programmatically.
Installing Required Libraries
pip install gspread google-auth google-auth-oauthlib google-auth-httplib2 googleapiclient
gspread
– Handles Google Sheets.googleapiclient
– Works with Google Docs and Slides.google-auth
– Manages authentication for Google APIs.
Setting Up Google API Credentials
- Go to Google Cloud Console.
- Enable Google Drive API, Google Sheets API, and Google Docs API.
- Create credentials (OAuth 2.0 client ID or Service Account).
- Download the JSON key file and store it in the working directory.
Automating Google Sheets
Authenticating and Connecting to Google Sheets
import gspread
from google.oauth2.service_account import Credentials
# Load credentials
creds = Credentials.from_service_account_file("credentials.json", scopes=["https://www.googleapis.com/auth/spreadsheets"])
# Connect to Google Sheets
client = gspread.authorize(creds)
# Open spreadsheet
sheet = client.open("Sales Data").sheet1
# Read data
data = sheet.get_all_records()
print(data)
Writing Data to Google Sheets
# Append new row
sheet.append_row(["Laptop", "150", "$75,000"])
print("Data added successfully.")
Updating and Deleting Data
# Update a specific cell
sheet.update("B2", "200")
# Delete a row
sheet.delete_row(3)
print("Sheet updated successfully.")
Automating Google Docs
Authenticating and Creating a Google Doc
from googleapiclient.discovery import build
# Connect to Google Docs API
docs_service = build("docs", "v1", credentials=creds)
# Create a new document
document = docs_service.documents().create(body={"title": "Automated Report"}).execute()
doc_id = document["documentId"]
print(f"Document created with ID: {doc_id}")
Adding Text to Google Docs
requests = [
{"insertText": {"location": {"index": 1}, "text": "This is an automated report.\n"}}
]
docs_service.documents().batchUpdate(documentId=doc_id, body={"requests": requests}).execute()
print("Text added to document.")
Automating Google Slides
Creating a New Google Slides Presentation
slides_service = build("slides", "v1", credentials=creds)
# Create new presentation
presentation = slides_service.presentations().create(body={"title": "Automated Slides"}).execute()
presentation_id = presentation["presentationId"]
print(f"Presentation created with ID: {presentation_id}")
Adding a Slide with Text
requests = [
{
"createSlide": {
"slideLayoutReference": {"predefinedLayout": "TITLE_AND_BODY"}
}
}
]
slides_service.presentations().batchUpdate(presentationId=presentation_id, body={"requests": requests}).execute()
print("Slide added successfully.")
Adding an Image to a Slide
requests = [
{
"createImage": {
"url": "https://example.com/image.png",
"elementProperties": {
"pageObjectId": "p1",
"size": {"height": {"magnitude": 300, "unit": "PT"}, "width": {"magnitude": 300, "unit": "PT"}},
"transform": {"scaleX": 1, "scaleY": 1, "translateX": 100, "translateY": 100, "unit": "PT"}
}
}
}
]
slides_service.presentations().batchUpdate(presentationId=presentation_id, body={"requests": requests}).execute()
print("Image added successfully.")
Automating Email Sending of Google Docs, Sheets, and Slides
import yagmail
yag = yagmail.SMTP("your_email@gmail.com", "your_password")
# Send the Google Docs link
yag.send(
to="recipient@example.com",
subject="Automated Google Docs Report",
contents="Here is the link to your report: https://docs.google.com/document/d/" + doc_id
)
print("Email sent successfully.")
Conclusion
This section covered automating Google Docs, Sheets, and Slides using Python, including creating, updating, and managing documents. These techniques are useful for automating reports, business documents, and presentations.
Would you like additional examples or modifications?