Adding items to a LO Writer table via bash script

My goal is to generate a valid FODT document from an FODT template file and values passed from a bash script. More specifically I’d like to insert values into rows of a table with two columns. Rows should be added to the table depending on how many new items are being passed from the script.

My FODT template file contains a table with just one row:

   <table:table table:name="table1" table:style-name="table1">
    <table:table-column table:style-name="table1.A"/>
    <table:table-column table:style-name="table1.B"/>
    <table:table-row>
     <table:table-cell table:style-name="table1.A1" office:value-type="string">
      <text:p text:style-name="P3">First Column</text:p>
     </table:table-cell>
     <table:table-cell table:style-name="table1.B1" office:value-type="string">
      <text:p text:style-name="P3">Second Column</text:p>
     </table:table-cell>
    </table:table-row>
   </table:table>

From a first glance, the script should just generate additional table-row xml nodes with the respective values passed via the script.

Is there a best practice to achieve this result?

Intuitively, sed would not be the first choice given its limitations handling the necessary newline and other special characters to generate XML code.

Use a plain text table (csv).

1 Like

This would be the easiest way even if you’re final file must be a FODT. It would be easier to add to a CSV and have a script convert it to FODT once the new info was added than to muck around trying to insert data into the middle of a file.

csv can be connected with a Base document from where you can generate reports in all supported text formats including fodt.

XML ignores white space. Of course, sed is an adequate tool.

If you are used to sed, you may use it. I have a project where I use an awk-skript, but to create this new I would use Python.
.
If you can control the input, you may generate a line-structure, wich can be editrd easily with sed/swk/Python.

As @Villeroy already suggested: Not editing an existing .fodt, but creating a template wich queries a csv-database from Calc or writes a Base-report (= defaults to Writer-table. Then save the result (if necessary as .fodt)

Thanks for your suggestions. As far as I see it is not a solution for my specific problem at hand. Or are you suggesting to integrate the CSV file somehow into the FODT file? Is that possible? If yes, how?

Thanks for that hint. I’ll check it out; that’ll probably make it much easier then to mess around with special characters in sed.

Interesting. Is it publicly accessible?

That sounds like quite a detour from the original problem though. My bash script is already quite functional with sourcing key-value pairs from env files to automatically create FODT documents replacing placeholder values from the template file. I’d really like to stick to that already existing bash script approach.

It is one possible way to get the job done without coding, just by proper configuration.
You may use a script to replace or edit the csv file in question, open the database document run the report and save it as fodt.
menu:File>New>Database…
[X] Connect to existing database
Type: Text
Specify the directory where you store csv files and the details (encoding, delimiters etc.).
After saving the new database document, all csv files in the specified directory appear as tables of a pseudo-database.
You may add queries to filter, sort and/or change column order of a table.
Right-click a table or query, choose “Report wizard” and let the wizard create a tabular report.
Save the database document.
Every time when you open the database document and double-click the embedded report, you get a text document with a table showing the content of the linked csv file. This text document can be saved in all available text formats, including fodt.

How you update or replace that csv file is up to you. But the document creation happens on the fly by running the report.