Generating Files From a Template Using Python

I want to create dedicated pages on my website for podcast episodes along with a transcript. 20+ episodes suggest this is a task to automate. I want to create dedicated episode pages in Hugo. I created a template file called template.md with the front matter and a placeholder for future content.

template.md

---
title: {{title}}
breadcrumb: {{title}}
Date: {{date}}
---

[Podcast]({{url}})

## Transcript

Next, I need the titles, dates, and URLs of the podcast to display. The Anchor.fm podcast feed has this information. I chose not to automate this part of the task. I copy the XML into Sublime Text. then use the find regex for <tag>.*?</tag> for each property and save three files for title, date, and URLs.

It’s been a while since I’ve used Python. I glimpsed a couple of tutorials that assume you’re using Jinja with Flask. There were a couple of kinks to work through. Excuse the code if it is not Pythonic. Python is not my daily driver language.

If you do not already have Jinja2 installed you can run python -m pip install jinja2.

from jinja2 import Template
names = None
urls = None
dates = None
with open("names.txt") as f:
    names = [x.strip() for x in f.readlines()]
with open("urls.txt") as f:
    urls = [x.strip() for x in f.readlines()]
with open("dates.txt") as f:
    dates = [x.strip() for x in f.readlines()]

We have to tell Jinja where to expect to find the template file before using the Template we imported. Here, I am using “.” to say the template is in the same folder as the code I am running. I am pulling the content of each file and setting for a variable along with removing extra new line characters.

from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader("."),
    autoescape=select_autoescape()
)
t = env.get_template("template.md")

I use the FileSystemLoader because I want to use the template.md I created.

combined = zip(names,urls, dates)
for title,url, date in combined:
    render = t.render(title=title, date=date, url=url)
    with open(f"{titles.lower()}.md","w") as f:
        f.write(render)

I am zipping all three variables into one structure, a triple in this case. I am rendering the template and saving the files to the same folder in each iteration. You might consider saving to a separate folder in case something breaks you can clean up faster.

Summary

Using Jinja2 and a template file we generated 20+ files based on three files that stored matching in order titles, dates, and URLs. This would have been a lot of typing manually and creating files if we did not automate the task.

from jinja2 import Template
names = None
urls = None
dates = None
with open("names.txt") as f:
    names = [x.strip() for x in f.readlines()]
with open("urls.txt") as f:
    urls = [x.strip() for x in f.readlines()]
with open("dates.txt") as f:
    dates = [x.strip() for x in f.readlines()]
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
    loader=FileSystemLoader("."),
    autoescape=select_autoescape()
)
t = env.get_template("template.md")

combined = zip(names,urls, dates)
for title,url, date in combined:
    render = t.render(title=title, date=date, url=url)
    with open(f"{titles.lower()}.md","w") as f:
        f.write(render)