4

I created a script for Ubuntu terminal that generates answers. The answers are copied to a LibreOffice Writer document. There are tables and a desired format for it. All the format will be lost if I overwrite, and I can't append if I don't output first. Is it possible for me to automatically insert the answers inside the tables?

I only have simple code like cat hostname to preview the machine hostname. I'm struggling in copying it and pasting. All I want is to know if it is possible to automatically direct the answer inside a table of a LibreOffice Writer document. Sample ODT template is uploaded below.

Sample Template

  • 1
    A libre office document is a zipped file. Unzip it. Find the data in the XHTML and you can edit your new content into the document. Zip it back up and open the file. Done it a couple of times. See https://askubuntu.com/questions/841970/edit-libreoffice-files-from-command-line on how to start; that answer uses calc but writer uses the same setup with some more mark-up. Only your own imagination is the limit ;-) – Rinzwind May 02 '18 at 08:21
  • 1
    You may get better answers on this topic on https://ask.libreoffice.org/ (same model but not affiliated with Stack Exchange). – David Foerster May 02 '18 at 08:40

1 Answers1

5

This doesn't answer the question because AFAIK there is no simple way to achieve the goal exactly like OP requests. Instead I show two different approaches to achieve the goal as best as possible.

As far as I know, the only easily accessible interface between a shell script and tables in GUI office software like LibreOffice is .csv

a comma-separated values file

This is very simple, but also very basic and doesn't allow for any formatting. Here's a usage example:

$ echo -e "heading 01,heading 02\ncell A2,cell B2\ncell A3,cell B3" >a.csv
$ loffice -o a.csv

LibreOffice csv 01

As a .csv is a simple text file, you can change the content with every editor, let's use sed:

$ sed -i 's/cell A2/replaced cell/' a.csv 
$ loffice -o a.csv

LibreOffice csv 02

As stated above, this doesn't allow for any formatting besides all caps, which in your context is not very satisfactory.


If you on the one hand want to have simple text files you can easily change with command-line tools, but on the other hand also want to get a beautifully formatted document,

LaTeX is for you

I'd create a template.tex and mark the lines I'd like to change later with comments LaTeX ignores, e.g. cell coordinates like %A2:

\documentclass{scrartcl}
\usepackage{booktabs}
\usepackage{multicol}
\begin{document}
\begin{tabular}{p{5cm}p{5cm}}
  \toprule
  \multicolumn{2}{l}{System/Client Information} \\
  \midrule
  {\tiny Machine Hostname:}\newline
  dessert's plowhorse %A2
  &
  {\tiny Machine Brand and Model:}\newline
  Thinkpad X240 %B2
  \\
  {\tiny Operating System (Version \& Variant):}\newline
  Lubuntu 16.04.3 LTS %A3
  &
  {\tiny Kernel Version\,/\,Update Level:}\newline
  4.10.0-35-generic %B3
  \\
  \bottomrule
\end{tabular}
\end{document}

Compiled this looks like:

LaTeX example 01

At the beginning of your script you just create a unique output filename and copy the template:

outfile="/path/to/$(date +%F_%T).tex"
cp /path/to/template.tex "$outfile"

Now you can change this file and adapt it to your needs with sed -i, thanks to the line marks added in comments it's easy to select single cells:

sed -i 's/.*%A2/  replaced cell value/' "$outfile"

When all changes are done just run pdflatex on the .tex file to get the .pdf output:

pdflatex "$outfile"

LaTeX example 02

Some links to get started with LaTeX

dessert
  • 39,982