Edit Libreoffice-Files from Command Line
I want to add lines to a libreoffice calc table from command line. But I have no idea how.
I just figured out how to start libreoffice from command line.
For example what iam looking for:
-The test.ods-file
before
Name Text
Hans Bla
Christian BlaBlub
I enter...
ubuntu> [a command -insert] Alf|test -file=test.ods
so that "Alf" and "test" is added as next line in the table.
-The test.ods-file
after:
Name Text
Hans Bla
Christian BlaBlub
Alf test
Solution 1:
.ods
is an archive. So you will need to extract the archive.
From the documentation:
XML file structure
Documents in OpenDocument file format are stored as compressed zip archives that contain XML files. To view these XML files, you can open the OpenDocument file with an unzip program. The following files and directories are contained within the OpenDocument files:
- The text content of the document is located in content.xml.
So it is not as simple as
[a command -insert] Alf|test -file=test.ods
since you also need to insert the XML parts.
$ cd ~/tmp/
$ unzip ../test.ods
Archive: test.ods
extracting: mimetype
extracting: Thumbnails/thumbnail.png
inflating: settings.xml
inflating: content.xml
inflating: meta.xml
inflating: styles.xml
inflating: manifest.rdf
creating: Configurations2/images/Bitmaps/
creating: Configurations2/toolpanel/
creating: Configurations2/progressbar/
inflating: Configurations2/accelerator/current.xml
creating: Configurations2/floater/
creating: Configurations2/statusbar/
creating: Configurations2/toolbar/
creating: Configurations2/popupmenu/
creating: Configurations2/menubar/
inflating: META-INF/manifest.xml
When you look at content.xml and you want to add a new row below the last one you would need to add something like this ...
<table:table-row table:style-name="ro1">
<table:table-cell office:value-type="string" calcext:value-type="string"><text:p>a2a2a2</text:p>
</table:table-cell><table:table-cell office:value-type="string" calcext:value-type="string">
<text:p>b2b2b2</text:p>
</table:table-cell></table:table-row>
before
<table:named-expressions/>
and then zip the files again (zip -r ../test2.ods .
from the directory with the files).
Result:
To edit the file from command line I used this command. I put the example in ~/Downloads and made a tmp/ in there to test. All the commands used for this:
cd ~/Downloads/tmp/
unzip ../test.ods
sed 's#</table:table><table:named-expressions/>#<table:table-row table:style-name="ro1"><table:table-cell office:value-type="string" calcext:value-type="string"><text:p>a3a3a3</text:p></table:table-cell><table:table-cell office:value-type="string" calcext:value-type="string"><text:p>b3b3b3</text:p></table:table-cell></table:table-row>&#' content.xml > content2.xml
mv content2.xml content.xml
zip -r ../test2.ods .
All you need to do is replace the text segments with your own.
Newer version courtesy of Terdon (it uses variables to make it better readable):
$ from="</table:table><table:named-expressions/>"
$ to="<table:table-row table:style-name="ro1"><table:table-cell office:value-type="string" calcext:value-type="string"><text:p>a3a3a3</text:p></table:table-cell><table:table-cell office:value-type="string" calcext:value-type="string"><text:p>b3b3b3</text:p></table:table-cell></table:table-row>"
$ sed "s#$from#$to$from#" content.xml
The "#" is a separator. If you have a "#" in the text use something else.