Test LibreOffice automatically
To the Work
To start gently, the script begins with a remarkably simple functional test of the Writer components (i.e., the Office package's word processing). Additionally, the script may write the text "Hello world!" in a new document. However, you need to open a new document for this. You can do so using the desktop
object generated above and the loadComponentFromURL()
method (line 59). This method can create new documents and also open existing files, which is explained in the next section.
The first argument for the function is a URL that points to the document to open. Here you need to create a blank document. The special URL for this is private:factory/swriter
. The document is to be in a new window and receives the frame
parameter as a second argument with the _blank
value for a new window.
The third parameter isn't needed in this situation; it is therefore set to
. This is the way to look for an existing LibreOffice window if the second parameter doesn't generate a new window by specifying _blank
.
The fourth argument is an empty tuple. Tester can specify parameters that influence how the document is opened. It could be opened as read-only or by specifying the password of a protected file.
Hello World!
If the document is created and stored under the document
variables, the text contents can be accessed via the Text
attribute. LibreOffice is cursor-based and adds new text inputs at the point where the cursor is positioned. To write in the document, the script must access the cursor; it gets it via the Text
attribute with document.Text.createTextCursor()
in line 65.
The next line of code changes the current style and sets the cursor attribute ParaStyleName
to Heading 1
which corresponds to the manual selection of the "Heading 1" formatting.
The script adds the text "Hello world!" at the point where the cursor is positioned via document.Text.insertString()
in the next line (Figure 1). The cursor itself must be specified as the first argument, then the text to be added. The third argument indicates whether the new text should overwrite the old or whether it should be inserted. The zero in line 67 is for adding text.
This should be enough for the first brief test; however, a review of the result is still missing. The script uses a trivial approach in this first test: Only the text content of the generated document is submitted to the automated test.
Some Text Please
The script in line 69 first generates a temporary file for the text export via NamedTemporaryFile()
in w+
mode, which makes it possible to write and read the file simultaneously. The script also sets the file encoding to utf-8-sig
. Python therefore knows that the file contains UTF-8 bytes and a leading byte order mark (BOM). That is the format in which LibreOffice exports text files. Further details are explained in the box "Other Export Filters for Calc and Impress."
Other Export Filters for Calc and Impress
Calc and Impress, the table and presentation programs from the LibreOffice suite, do not allow text-only export to a .txt.
file. There are other text-like export formats for this, such as CSV for Calc spreadsheets or HTML for Impress presentations. They can use the script for an automated comparison, thereby allowing the same approach as in the test of Writer documents.
Two PropertyValues
must be specified to export a Calc document in CSV:
csvFilterName = PropertyValue() csvFilterName.Name = 'FilterName' csvFilterName.Value = 'Text - txt - csv (StarCalc)'csvFilterOptions = PropertyValue() csvFilterOptions.Name = 'FilterOptions' csvFilterOptions.Value = '44,34,0'
The information for FilterName
is requested again first, Text - txt - csv (StarCalc)
in this example. The second PropertyValue
is necessary because the CSV export requires a few parameters, namely the specification of the field separator, the field boundary and the character set of the file to be exported.
The second PropertyValue
is called FilterOptions
and its value is 44,34,0
. The latter sets the field separator for the comma (ASCII code 44) and the field boundary for double quotes (ASCII code 34). The closing zero selects the default character set for the system which should normally be UTF-8.
The two property values must then be specified for the actual export:
document.storeToURL(fullPath, (csvFilterName,csvFilterOptions,))
To export Impress documents in HTML, however, only a PropertyValue
is required (like with Writer). It must be called FilterName
, and its value must be impress_html_Export
.
The script also needs a slightly different exporter name for the PDF export if it is a Calc or Impress document. For example, the PropertyValue
must have the appropriate value calc_pdf_Export
for Calc and the value impress_pdf_Export
for Impress.
In the following five lines, the script then instructs LibreOffice to export the current document as a text file into the temporary file. For this, it first designates the export filter by generating a PropertyValue
object called FilterName
and the value Text
(lines 71 to 73).
Line 75 subsequently directs LibreOffice to store the current document under the specified URL with document.storeToURL()
. The URL is made up of the prefix file://
and the full path to the temporary file (txtFile.name
). This function gets a list of PropertyValue
objects as an additional argument. This is only a reference to the export filter through which LibreOffice knows that it is supposed to export the contents of the document as a text file.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.