banner

For a full list of BASHing data blog posts see the index page.  RSS


Building an ODT on the command line

One of my routine tasks involves getting several strings of information from a particular website, then using those strings to

  1. add an entry to a tasks table
  2. add items in standardised places in a template ODT
  3. give the ODT a filename based on the strings

Answering my own question Isn't there an easier way to do this?, I wrote a shell script that starts with a YAD form dialog. After I enter the strings in their appropriate form boxes, the script does the three jobs listed above automatically.

An unusual aspect of my script is that it creates a formatted ODT without involving a GUI word processor. To do this I use LibreOffice Writer to convert an HTML file to an ODT on the command line. The HTML doesn't have to be up-to-date, either, or use CSS for styling. You and I may have forgotten (or never knew) the HTML of 25 years ago, but it's apparently still in the Writer code base. The following primitive HTML file "sample.html" with its long-deprecated tags is correctly displayed as a kind of "proto-ODT" when opened in Writer:

<html><p><font face="Liberation Sans" size="5" color="red">First line</font><br><font face="Times New Roman" size="3" color="blue"><b><i>Second line</i></b></font></p></html>

ODT1

Here's a made-up example with CSS, and its appearance in Writer:

<html><head><style>p.top {font-family:"Comic Sans MS";font-size:36px;margin:10px;text-align:center;}p.bottom {font-family:Verdana;font-size:18px;margin:20px;text-align:center;}</style></head><body><p class="top">Don't forget...</p><p class="bottom">Your {car} is due for its {odo} km service on {date}. Ring us on 6499 2136 to book a service, or book online at <a href="https://www.yourcarmechanic.com">yourcarmechanic.com</a>.<br />Have a question? Ring {mech} today on {mechsmobile}.<br />Here are the service jobs we plan to do:<br /><br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................</p></body></html>

ODT2

In the following demonstration script I've given particular values to the placeholder variables, but those values could come (as they do in my daily-work script) from other sources, such as entries in a YAD dialog.

printf prints the HTML code with the given values for the shell variables to "reminder.html". Notice that quotes (") are escaped with backslashes.
lowriter --headless --convert-to odt reminder.html uses Writer to convert "reminder.html" to "reminder.odt". The headless option means that Writer does this without opening a GUI.
lowriter --nologo -o reminder.odt & disown. Writer is here launched without a splash screen (nologo) and opens "reminder.odt". The BASH built-in "disown" command follows, to allow the shell to disengage from that last lowriter command and go on to the next command in the script.
rm reminder.html. The HTML file is deleted, as it's no longer needed.

#!/bin/bash
car="Commodore"
odo="50000"
date="Wednesday, 19 January 2022"
mech="Dwayne"
mechsmobile="0999 222 333"
 
printf "<html><head><style>p.top {font-family:\"Comic Sans MS\";font-size:36px;margin:10px;text-align:center;}p.bottom {font-family:Verdana;font-size:18px;margin:20px;text-align:center;}</style></head><body><p class=\"top\">Don't forget...</p><p class=\"bottom\">Your $car is due for its $odo km service on $date. Ring us on 6499 2136 to book a service, or book online at <a href=\"https://www.yourcarmechanic.com\">yourcarmechanic.com</a>.<br />Have a question? Ring $mech today on $mechsmobile.<br />Here are the service jobs we plan to do:<br /><br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................<br />........................................................................</p></body></html>" > reminder.html
 
lowriter --headless --convert-to odt reminder.html
lowriter --nologo -o reminder.odt & disown
rm reminder.html
exit 0

And here's "reminder.odt" in Writer as generated by the script:

ODT3

A new terminal for 2022. For some years now my terminal emulator has been gnome-terminal. After looking through the list of emulators on the invaluable Linux Links website and trying a few of the newer ones, I installed sakura (from my distro's repository) and was really impressed with its speed and simplicity.

To tweak sakura I chose Bitstream Vera Sans Mono Roman as the terminal font, size 11, background #E6E6DC and text black. To open sakura with the window size I prefer with that font, I launch the program as sakura -c 140 -r 55 with a keyboard shortcut. Unlike gnome-terminal, sakura doesn't have the option of defining its window position with a GTK "geometry" setting. To get the window to open at top left on my display, I disabled the "By default, place windows: At the centre of the screen" option in my Xfce Window Manager Tweaks/Placement dialog.


Last update: 2021-12-29
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License