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
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.