Andrew Shay logo
Blog & Digital Garden
Home > Blog > Auto Magor: Python Program for...

Auto Magor: Python Program for Generating Print Magazines

2024-02-29

What is Auto Magor

Auto Magor is a simple Python program that I created that can generate 3 column print magazines from text files and images.
The user just has to create simple text files with the article body, metadata about it, and an article image.
That is then turned into a 3 column magazine, as a pdf, that can be printed at home.

I created it because I have been trying to use my phone less at night before going to sleep. I don't like reading on my phone, and my eBook is alright, but the blue light still isn't great.
Reading physical books and magazines is really nice though. However, there is a still a lot of online content that I'd like to read offline.
I thought, if only I could take that online content and make a magazine out of it 🤔
Making it look like a print magazine (instead of just printing webpages outright) is just more fun.

If I want it, and it can be built, I must build it.

How Auto Magor Works

Auto Magor is surprisingly simple, in general.

Articles are stored in simple text files with the following format.
title:The Title of This Article
subtitle:A subtitle for this Article
author:Foo Bar
date:January 1, 2024
image:article_01.png
source: Text that will appear after the date
body:
The first sentence of the first paragraph. The second sentence of the first paragraph.

The second paragraph is here. Another sentence in the second paragraph.

The Pillow library is used to generate each page.
Each column of text is drawn onto the image once the column is full of text.
A column of text is generated by appending one word at a time and calculating if it fits on the current line.
Once the current line is full, the next line is started. This continues until the max column height is reached, and then the next column is started.
I use the method draw.multiline_textbbox() to get the dimensions before having to actually draw the text.
This way of generating columns makes Auto Magor very slow though.

It's simple, but along the way I ran into a few things that were a little tricky at first...

The first thing I hit, was that some of the last lines of a paragraph were unexpectedly reaching the max column height, mid-sentence.
I realized that it was caused by "low hanging" characters like pqgy,).
Before writing a new line, I check if it will be able to fit.
At first, I was (re)checking the height as each word was added. This was just for simplicity. So, once a word with a low hanging was attempted, it would push to the next line.
I had to change it to test the new line height one time, and with the low hanging characters instead.

I also had to start tracking when I was at the start of a new column.
Blank lines between paragraphs in the article files were pushing the column start down.
New columns were also being started with a space since I was appending with "a space" followed by the next word.

How to Make Auto Magor Faster

Since I won't be running Auto Magor very often, I prefer code simplicity to execution speed.
But there are some things that I assume could make Auto Magor faster.

I get the width of the column by appending the word, then calculating the new column width.
But I do this for every word. So, it's constantly recalculating the line. i.e. every word that has already been applied.
I think I could maintain a width variable manually, get the width of each word by itself, and increment it until the max width is reached.

If the above works, a cache of the widths for the most frequent words could be generated, and then maintained across runs for a given font and size.



Download Auto Magor

Auto Magor logo