Daily Note templates for each weekday

Thanks @osgav - this is a bit more complicated than I hoped, but it works. Here is the Python script that I came up with. It can also be used for creating other dynamic content in daily notes.

#!/usr/bin/python3

"""Command for loading daily journal content depending.

Use this as a user defined template with the Templater plugin.

Set the template folder location in this script and in the
Templater plugin settings. You can put this script inside the
template folder and configure it as user template.

If the template folder is called "Templates", and this script
is called "journal.py", add this Templater user function:
journal = c:\python39\python.exe Templates\journal.py

Then add <% tp.user.journal() %> to your normal daily notes template.

Add templates with names like "Schedule for Monday.md" etc.
to your template folder. These will then be inserted according
to the workday.

You can also add other functions for creating dynamic content
and call these in the main() function.
"""

import sys
import traceback
from datetime import date
from pathlib import Path

template_folder = 'Templates'
template_name = 'Schedule for {weekday}.md'


def print_schedule_for_weekday():
    today = date.today()
    weekday = today.strftime('%A')
    filename = template_name.format(weekday=weekday)
    path = Path(template_folder) / filename
    try:
        with open(path, encoding='utf-8') as f:
            content = f.read()
        content = content.strip()
    except FileNotFoundError:
        try:
            with open(path, 'w', encoding='utf-8') as f:
                f.write(f'## Schedule for {weekday}\n'
                        'Please fill in the details.\n')
            content = (f"Created an empty template [[{filename}]],"
                    ' since no existing weekday template was found.')
        except Exception:
            content = ("Weekday template not found, and cannot be created."
                       " Are you sure the template folder exists?")
    if content:
        print()
        print(content)
        print()


def main():
    if sys.stdout.encoding != 'utf-8':
        sys.stdout.reconfigure(encoding='utf-8')
    print_schedule_for_weekday()


if __name__ == '__main__':
    try:
        main()
    except Exception:
        print("Error when running user template.")
        print("```")
        traceback.print_exc(file=sys.stdout)
        print("```")

Note: Updated for the new Templater syntax.

4 Likes