Nathan Grigg

Day One Export

Here is a Python script to export Day One journal entries into any text-based format (such as html, markdown, plain text, or Latex).

Install
pip  install  dayone_export
View on github
nathangrigg/dayone_export
Download
nathangrigg-dayone_export-latest.zip
Documentation
Read the Docs

Note: the Day One Mac app can export to plain text, and in the future the iOS apps will export to PDF. This is for people who need or want extra customization options.

My Day One journal

I have always been a fan of the Day One Mac and iOS Apps. I was keeping an electronic journal before Day One came along (in Latex, if you must know), but I quickly changed my ways. Day One makes things easier and more fun.

I switched to Day One quickly, but not without an abundance of caution. The last thing I want is for my journal to be unreadable 5 years from now. I was reassured to see that Day One stores each journal entry as a plist file. Furthermore, the entry itself is 100% certified organic plain text. The rest of the plist is just metadata.

As time passed, Day One added features. Most recently, they added the ability to include photos, location information, and weather. All this talk about new features scared me, because more features almost always means more complication. In this case, there was a good chance the extra complication would make my data less future-proof. But in the end, there was no need for me to worry. These guys are good.

A Day One entry is still a simple plist file. The entry itself is still plain text. Location and weather are just more metadata. Best of all, photos can be included with but not inserted into an entry. There is no need for any markup within the journal entry saying “this is where the photo goes.” You don’t have to base64-encode or link to an external file or any of the other awful things word processors have done when dealing with images. A photo is just another piece of metadata that says “This photo goes with that journal entry.”

A Day One export tool

I put together a Python script to export my journal entries. It uses a Jinja template to combine my Day One entries into a single file of whatever format I want. A simple template and a few lines of css turned my journal into enough html to fill 80 printed pages:

I wrote it for myself, but I thought others might find it useful, so I have posted it on github.

Reading plist files in Python

This part is easy. Just import plistlib and do

plistlib.readPlist(filename)

Using Jinja templates in Python

This is more complicated, and the main thing I learned by making this script. The following script fills in a Jinja template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from jinja2 import Environment, FileSystemLoader
import os

# j contains list of entries, template is the name of the template file,
# and markdown_filter is a function which converts markdown to html
path, base = os.path.split(template)
env = Environment(loader=FileSystemLoader(path), trim_blocks=True)
env.filters['markdown'] = markdown_filter
template = env.get_template(base)
output = template.render(journal=j)

Loading the template file was a little confusing. First, you set up the environment with a FileSystemLoader in line 7, which takes one or more search paths as an argument. Then the get_template command in line 9 searches through all of the paths for the template you are looking for. I could not find a way to just load an arbitrary template file, hence the awkward workaround in line 6.

Writing Jinja templates

If you’ve ever used Liquid or any other templating language, Jinja looks pretty familiar. Here is a basic template to produce an html version of my journal:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html>
<head>
    <title>Journal Entries</title>
    <link rel="stylesheet" href="style.css" type="text/css">
    <meta charset="UTF-8" />
</head>
<body>
<h1 class="page-title">Journal Entries</h1>
{% for entry in journal %}
<article class="entry">
    <h1 class="entry-title">{{ entry['Date'].strftime('%A, %b %e, %Y') }}</h1>
    <p class="location time">
        {% if 'Location' in entry %}
        {{ entry.place(ignore="United States") }},
        {% endif %}
        {{ entry['Date'].strftime("%-I:%M %p %Z") }}
    </p>
    <div class="entry-text">
        {% if 'Photo' in entry %}
        <img class="entry-photo" src="{{ entry['Photo']  }}"/>
        {% endif %}
        {{ entry['Text'] | markdown }}
    </div>
</article>
{% endfor %}
</body>
</html>

Control statements go inside {%...%} blocks, like the for loop from line 10 to line 26 which loops over all the entries. Recall that the journal variable is passed to the template from Python (in line 10 of the Python code above).

Variables are inserted into the document using {{...}} blocks, like the date on line 12 and the photo on line 21. Jinja allows a lot of Python into variable blocks (more than Liquid allows Ruby), which means I can call strftime to format the date in the way that I want. You see more of this on line 14 with the in in the if block, and on line 15 which uses the place method of the entry object.

Line 23 shows how to apply a filter to a variable. The text of the variable passed through the markdown filter. This is a custom filter defined in my Python script, but there are also several built-in filters.