Last active
January 21, 2025 00:50
-
-
Save pbinkley/4e86f88e15344f99c7c31b03c3329c02 to your computer and use it in GitHub Desktop.
Python script to generate a weekly note for Obsidian, with embedded Google Calendars for each day and inclusion of Obsidian tasks using dataview plug-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
from string import Template | |
import os.path | |
# create Weekly Notes file for current week | |
# - replaces the Periodic Notes plugin, which stopped working | |
# correctly for me in Jan. 2025 (it kept using the year 2024) | |
# - to run: open terminal, cd to "Obsidian environment/Scripts", | |
# run "python3 weekly.py" | |
header = """[[Google Log-in]] | |
[Consultations](#^consultations) | [Projects](#^projects) | |
[Monday](#^monday) | [Tuesday](#^tuesday) | [Wednesday](#^wednesday) | [Thursday](#^thursday) | [Friday](#^friday) | |
## Consultations ^consultations | |
### With tasks | |
```dataview | |
TASK | |
FROM "Consultations - Active" | |
WHERE !completed AND !checked | |
GROUP BY file.name | |
``` | |
### No tasks | |
```dataview | |
LIST | |
FROM "Consultations - Active" | |
WHERE all(file.tasks.fullyCompleted) OR length(file.tasks) = 0 | |
``` | |
## Projects ^projects | |
### With tasks | |
```dataview | |
TASK | |
FROM "Projects - Active" | |
WHERE !completed AND !checked | |
GROUP BY file.name | |
``` | |
### No tasks | |
```dataview | |
LIST | |
FROM "Projects - Active" | |
WHERE all(file.tasks.fullyCompleted) OR length(file.tasks) = 0 | |
``` | |
""" | |
day_template = Template(""" | |
### $daytitle ^$dayname | |
<iframe src="https://calendar.google.com/calendar/embed?height=600&wkst=1&bgcolor=%23ffffff&ctz=America%2FEdmonton&showPrint=0&mode=AGENDA&showTz=0&showCalendars=0&showTabs=0&showTitle=0&showNav=0&src=[GOOGLE_SRC]&color=%23039BE5&dates=$ymd/$ymd" style="border:solid 1px #777; float: right; clear: right; margin-left: 1em;" width="200" height="600" frameborder="0" scrolling="no"></iframe> | |
- | |
- | |
#### Tasks: $monday_task | |
```dataview | |
TASK | |
WHERE due = date("$ymddash") or scheduled = date("$ymddash") or completion = date("$ymddash") | |
``` | |
#### Changed today: | |
```dataview | |
LIST modified | |
FROM !"Weekly Notes" | |
WHERE modified AND modified.year = $thisyear AND modified.month = $thismonth AND modified.day = $thisday | |
``` | |
<hr> | |
""") | |
def generate_weekly_note(): | |
# Get the current date | |
now = datetime.datetime.now() | |
# Calculate the start of the week (Monday) | |
start_of_week = now - datetime.timedelta(days=now.weekday()) | |
week_num = '{0:02d}'.format(start_of_week.isocalendar()[1]) | |
title = f"Week {week_num} - {start_of_week.strftime('%b %d')}" | |
print (f"{title}") | |
destination = f"../../Weekly Notes/{now.strftime('%Y')}/{title}.md" | |
if os.path.isfile(destination): | |
exit(f"Quitting - {destination} already exists") | |
# Create a list of days in the week | |
days_in_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] | |
# Initialize the Markdown file | |
markdown_content = header | |
# Add sections for each day | |
for i, day in enumerate(days_in_week): | |
today = start_of_week + datetime.timedelta(days=i) | |
print(f"{today.strftime('%A %Y-%m-%d')}") | |
markdown_content += day_template.substitute( | |
daytitle = today.strftime('%A, %B %-d'), | |
dayname = today.strftime('%A').lower(), | |
ymd = today.strftime('%Y%m%d'), | |
ymddash = today.strftime('%Y-%m-%d'), | |
thisyear = today.strftime('%Y'), | |
thismonth = today.strftime('%-m'), | |
thisday = today.strftime('%-d'), | |
monday_task = "\n- [ ] [[Publicizing Workshops]]" if today.weekday() == 0 else "" | |
) | |
# Save the Markdown file | |
with open(f"{destination}", "w") as file: | |
file.write(markdown_content) | |
# Run the script to generate the weekly note | |
generate_weekly_note() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment