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.

Figure 1: The "Hello world!" sentence commanded via script appears in the Writer without the --headless option.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • LibreOffice Macros with ScriptForge

    ScriptForge helps you automate LibreOffice by building portable macros.

  • Text to Speech

    Visually impaired users often find working with text and tables in office suites difficult. Pico TTS, a text-to-speech synthesizer, and the Read Text extension for LibreOffice and OpenOffice provide a solution.

  • LibreOffice 7 Now Available

    The LibreOffice 7 office suite is now available with important compatibility improvements.

  • The Clear Choice

    While LibreOffice and OpenOffice have a shared past, LibreOffice outstrips OpenOffice in contributors, code commits, and features.

  • LibreOffice 3.5

    PowerPoint shapes. We investigate how the two office modules work together.

comments powered by Disqus