LibreOffice: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(Removed the WIP (work in progress) tag.)
 
(63 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{WIP}}
This article uses material from the Wikipedia article "[https://en.wikipedia.org/wiki/LibreOffice LibreOffice]" and other Wikipedia articles, which are released under the [https://creativecommons.org/licenses/by-sa/4.0/ Creative Commons Attribution-Share-Alike License 4.0].
This article uses material from the Wikipedia article "[https://en.wikipedia.org/wiki/LibreOffice LibreOffice]" and other Wikipedia articles, which are released under the [https://creativecommons.org/licenses/by-sa/4.0/ Creative Commons Attribution-Share-Alike License 4.0].


==Introduction==
==Introduction==
This UDF ([https://www.autoitscript.com/forum/index.php?showtopic=210514 AU3LibreOffice]) aids the user in creating and manipulating LibreOffice Documents.<br>  
This UDF ([https://www.autoitscript.com/forum/index.php?showtopic=210514 AU3LibreOffice]) aids the user in creating and manipulating LibreOffice Documents.<br>  
This wiki article tries to explain basic principles of the UDF and offers tips on how to get started with automating LibreOffice (LO for brevity). It also points out some differences between automating the MS Office and LO suites. <br>
This wiki article tries to explain basic principles of the UDF and offers tips on how to get started with automating LibreOffice. It also points out some differences between automating the Microsoft Office and LibreOffice suites. <br>
It is not intended to explain LO's API.<br>
It is not intended to explain LibreOffice's API.<br>
To work with this UDF, you should be familiar with both AutoIt and LO.
To work with this UDF, you should be familiar with both AutoIt and LibreOffice.


===What is LibreOffice?===
===What is LibreOffice?===
Line 32: Line 31:


===LibreOffice Object Model===
===LibreOffice Object Model===
The LibreOffice UDF is a wrapper for the complex LibreOffice object model.  
The LibreOffice UDF is a wrapper for the complex LibreOffice object model (details can be found on this [[LibreOffice-Technical Comparison#COM_vs_UNO|wiki page]]).
It hides the complexity of the object model and replaces it with a few AutoIt function calls.<br>
It hides the complexity of the object model and replaces it with a few AutoIt function calls.<br>
The functions provided by the UDF cover most of the functionality that is typically required.<br>
The functions provided by the UDF cover most of the functionality that is typically required.
However, if you need more complex functions, you will have to write them yourself. First, you need to familiarize yourself with the [https://api.libreoffice.org/ LibreOffice API].<br>
However, if you need more complex functions, you will have to write them yourself. <br>
First, you need to familiarize yourself with the [https://api.libreoffice.org/ LibreOffice API].<br>
The following books will be a valuable help also in this line.
The following books will be a valuable help also in this line.
* [https://www.pitonyak.org/OOME_4_1.odt OpenOffice.org Macros Explained — OOME Fourth Edition]
* [https://www.pitonyak.org/OOME_4_1.odt OpenOffice.org Macros Explained — OOME Fourth Edition]
Line 41: Line 41:


===Technical comparison: LibreOffice vs Microsoft Office===
===Technical comparison: LibreOffice vs Microsoft Office===
For those users familiar with Microsoft Office and its automation API, we have included some comparisons between the two suites' APIs and internal behaviour.
For those users familiar with Microsoft Office and its automation API, we have included some comparisons between the two suites' APIs and internal behaviour on the [[LibreOffice-Technical_Comparison|following page]].


====Applications vs ServiceManager====
===Requirements===
In ''Microsoft Office'', you always begin with a Microsoft Word, Excel or Powerpoint Application object, which is an instance of the respective component without a Document, Book, or presentation opened in it yet. The application instance is then used to create, connect or open Documents, Books or Presentations, or to set global settings. The Application instance when created can be either visible or invisible.<p>
Currently there are no external requirements needed to run this UDF. You are able to work with both the '''Installed''' and also the '''Portable''' version of LibreOffice. However do take note of the methods followed to work with [[#How LibreOffice Portable is Initialized|Portable LibreOffice]] to make sure it will fit your requirements.
In ''LibreOffice'' you always begin with what is called a “ServiceManager”, this is essentially the equivalent of the Word, Excel or Powerpoint application Object, and is used to open, create or connect to Documents, Sheets or Presentations as needed. <br>
The ServiceManager, when created, has '''no visible UI'''.<p>
The UDFs included with AutoIt for ''MS Word'' and ''MS Excel'' include separate functions for creating an instance of the corresponding Application Object, _Word_Create and _Excel_Open, because the Application objects are unique for each component, e.g., an Excel Application is used solely for working with Excel files and settings, etc.<p>
In the ''LibreOffice'' UDF, the user is saved steps of initializing the ServiceManager, as the ServiceManager is used alike for all the different components of LibreOffice (Base, Calc, Writer, etc.). The ServiceManager is “aware” of all opened Documents in the several LibreOffice Components, and can create instances of any component type, as needed.<br>
It can also open any appropriate Document in the correct component as needed. When the user calls for the first time *_DocCreate, or *_DocConnect, or *_DocOpen, or any similar functions, the ServiceManager is initialized in the background, and stored as a static variable for use in the UDF when needed.  
<br> *****************************************


====Selections and Cursors====
===Limitations===
=====Word vs Writer=====
* The ability to automate LibreOffice Portable (and OpenOffice) has been added, and ''should'' work correctly. However to achieve this, it currently adds some temporary Registry entries to ''HKEY_CURRENT_USER'', unless there is an installed version of LibreOffice or OpenOffice present. See '''_LO_InitializePortable''' for more detail.
''MS Word'' uses two types of data selections, the first is called a “''Selection'', which is the visible selection you create in the Word application using your keyboard or mouse (or also using the API). The second type is called a “''Range'', and is an invisible or background selection of data, created solely using the API. Both support similar methods for extending or collapsing the selection, and have similar properties. A Range is able to be extended, or the beginning and ending position is able to be moved nearly the same as a Selection is, except a Range cannot be extended or moved by visual or on-screen lines, only structural units (paragraphs, sentences, etc.). There can be only one instance of a Selection at once, but you can have multiple Ranges at one time.<p>
* This UDF has been written using the English version of LibreOffice, and some aspects may only work for the English version of LibreOffice installations without errors.
In ''LO Writer'' you also have two primary types of data selections. The first type of selection or “''cursor''” as it is generally known, is internally known as a “''ViewCursor'', which is visible to the user. The second type of selection or “cursor” is called a "''TextCursor''", and is invisible to the user.<p>  
* Many functions in this UDF may or may not work with OpenOffice, and some are definitely for LibreOffice only. These unfortunately have not been documented.
A ''ViewCursor'' is what you see when you select some data in the document, and is also located wherever the caret is placed in the document. A ''ViewCursor'' is “aware” of characters, lines (where you see a line breaks to go to the next line to fit the Document width), pages and screens (your current view), as well as the start and end of the Document, it is "View dependent" for its usefulness. There can only be one ''ViewCursor'' existing in a document at one time. A ''ViewCursor'' can be used very similarly to a ''TextCursor'' for the purpose of inserting or retrieve text, inserting shapes or tables etc., as well as getting and setting properties.<p>
* For those using AutoIt versions older than '''3.3.16.1''' some functions in this UDF use Maps, which will not be recognized as proper syntax in older AutoIt versions.
The ''TextCursor'' on the other hand is always invisible to the user whether or not data is selected. The ''TextCursor'' is “blind” to lines, pages and screens. A ''TextCursor'' can, like a ''ViewCursor'' “see” characters, but it is also aware of the start and ending of words, sentences and paragraphs, it is more "document dependent" for its usefulness. Unlike a ''ViewCursor'', you can have as many ''TextCursors'' as needed in one document.<p>
 
There is a third type of Cursor that I will mention here briefly, it is called a "''Table Cursor''". It is, as you may have guessed, only used for manipulating Tables, whether for joining cells, coloring, or sizing rows and columns etc. A Table Cursor is only aware of the Table and its cells. It cannot transverse outside of the Table. It is aware of cells, and the beginning and ending of the Table. It can go be moved to a specific cell by name, and merge and split cells. It cannot insert or retrieve text, nor edit text attributes.<p>
===Special Terminology===
When retrieving the current selection(s) made by the user, i.e. a selected string of text, the resulting object is essentially identical to a TextCursor, and in this UDF a TextCursor is created matching the selection to be returned to lessen the number of cursor types to account for in other functions. If the selection is not just text, another type of Object will be returned, as described in the selection function.
Some terminology you will encounter frequently in the UDF, and sometimes in these articles, are the following (in alphabetical order):
 
; Component
: Any component or module of LibreOffice. They are: Base, Calc, Draw, Impress, Math, and Writer.
 
; Hundredths of a MM (HMM)
: As implied by the name itself, Hundredths of a MM is a hundredth part of a Millimeter, and is the measurement unit used by LibreOffice internally for nearly all widths, heights, distances, etc. For simplicity, this is the unit type used in this UDF as well, unless otherwise stated in the Parameters. A converter function has been supplied with this UDF, found in the “LibreOffice_Helper.au3" file, called '''_LO_UnitConvert'''. This function can convert from millimeters, centimeters or inches to HMM, and back again, depending on the flag called.  
 
; RGB color Integer
: A color unit you will find frequently in this UDF. LibreOffice nearly always uses this type of color value when setting or retrieving a color property. The RGB color integer is a number from 0 to 16,777,215, where each separate number represents a color. The relationship between RGB and a RGB Integer is based on a simple calculation: RGB Integer = Blue x (256 x 256) + Green x 256 + Red, or in Hexadecimal, 0x00RRGGBB, where RR = Red component (0-255), GG = Green component (0-255), BB = Blue component (0-255), and the highest byte (alpha) is unused and usually 0.
 
; Style Names
: LibreOffice uses Styles for various things, such as Paragraph or Character styling. LibreOffice automatically translates these style names according to the user's default language currently set, these are called the “Display Name”. To enable Macros, as well as LibreOffice itself to work on any language system these style names are also internally named so that they can remain constant. In the English version of LibreOffice the Display Name and the Internal name, or “Programmatic Name” of every style is in most cases the same. There are a handful of exceptions to this however, for example “Default Paragraph Style” is internally named “Standard”, “No Character Style” is also internally called “Standard”, (but is classed as a Character Style, not a Paragraph Style).<p>To aid the user in recognizing what Styles are which, each respective style name retrieval function (e.g. _LOWriter_ParStylesGetNames) offers the ability to still retrieve the Display name, which will be returned in the '''same''' order as the Internal names are returned, allowing the user to easily have a list of each name and see which belong to which. On top of this, each respective style name retrieval function also lists any styles that have names differing from their internal names in the Remarks section of the header.
 
; UI
: User Interface, the visual windows and buttons and tabs the user sees and is used to work with LibreOffice manually.
 
==Installation==
Place the entire set of .au3 files of this UDF into a directory searched by SciTE (details can be found in the AutoIt help file for keyword ''#include''). <br>
No further installation or files are required.
 
==Getting started==
With the exception of working with Portable LibreOffice (see the [[#How LibreOffice Portable is Initialized|Portable LibreOffice]] section regarding this), the first step will always be to acquire a Document object. <br>
Simply open an existing file, connect to an already opened document or create an entirely new document.<br>
Once a Document object is acquired, you then go forward from there to accomplish the task you desire to do.
<p>'''Example''': (<small>No error checking is integrated, as we want to give a quick impression of how easy it is to create an AutoIt script with the LibreOffice UDF.</small>)
<syntaxhighlight lang="autoit">
#include "..\LibreOfficeWriter.au3"
Global $oDoc, $oViewCursor
$oDoc = _LOWriter_DocCreate(True, False)                      ; Create a New, visible, Blank Libre Office Document.
$oViewCursor = _LOWriter_DocGetViewCursor($oDoc)              ; Retrieve the document view cursor to insert text with.
_LOWriter_DocInsertString($oDoc, $oViewCursor, "Hello World")  ; Insert some text at the ViewCursor.
_LOWriter_DocClose($oDoc, True)                                ; Close and Save the document as .odt document to the desktop
</syntaxhighlight>
 
===What do I do with all of these files?===
The LibreOffice UDF consists of a large number of files. How do you know which to include in your script? <br>
To begin with, the files are arranged in a semi-hierarchical form (3 levels: suite, module and object).
<p>Each filename consists of the string "'''LibreOffice'''" the optional '''module name''' and the optional '''object name'''. <br>
These optional parts of the file name (including description) are listed in the following tables. <br>
To help you decide which files to include in your script, examples are provided in the last table.
 
{| class="wikitable"
|+  Hierarchy of files
|-
! Level !! Naming Scheme !! Example filename !! What do you get with this example? !! Comment
|-
| suite level || LibreOffice.au3 || LibreOffice.au3 || All functions of all modules of the LibreOffice suite. || Use include files on this level only when you need '''ALL''' LibreOffice modules in your script.
|-
| module level || LibreOffice<Module>.au3 || LibreOfficeWriter.au3 || All functions of the LibreOffice Writer module. || That's the '''recommended approach''' for 99.99% of the scripts.
|-
| object level  || LibreOffice<Module><_Object>.au3 || LibreOfficeWriter_Doc.au3 || All functions related to a document object of the Writer module. || Use this level if you need to process a single object of a LibreOffice module.
|}
 
{| class="wikitable"
|+  Valid Modules
|-
! Module !! Description !! Module !! Description
|-
| Base || Database management program, similar to Microsoft Access. || Calc || Spreadsheet program, similar to Microsoft Excel.
|-
| Draw || Vector graphics editor, similar to Microsoft Visio. || Impress || Presentation program, similar to Microsoft PowerPoint.
|-
| Math || Formula editor for mathematical expressions. || Writer || Word processor program, similar to Microsoft Word.
|}
 
{| class="wikitable"
|+  Valid Objects
|-
! Object !! Description !! Object !! Description
|-
|| Cell || Manipulate cells in ''Calc'' and ''Writer''. || Char || Manipulate character styles in ''Writer''.
|-
|| Comments || Manipulate cell comments in ''Calc'' documents. || Constants || Constants for the respective module.
|-
|| Cursor || Perform cursor movements in ''Calc'' and ''Writer''. || Database || Manipulate ''Base'' Databases.  
|-
|| DirectFormatting || Manipulate Direct Character and Paragraph formatting in ''Writer''. || Doc || Manipulate documents in ''Base'', ''Calc'' and ''Writer''.
|-
|| Field || Manipulate ''Calc'' and ''Writer'' document fields. || FootEndNotes || Manipulate ''Writer'' Foot/End notes.
|-
|| Form || Manipulate ''Base'' Forms and ''Writer'' Forms and Form Controls. || Frame || Manipulate ''Writer'' Frames and Frame Styles.
|-
|| Helper || Manipulate data for use in various functions of the UDF. || Images || Insert and modify Images in ''Writer''.
|-
|| Internal || Internal functions not to be used by Scripters! || Num || Manipulate ''Writer'' Numbering Styles.
|-
|| Page || Manipulate ''Writer'' and ''Calc'' Page Styles. || Par || Manipulate ''Writer'' Paragraph Styles.
|-
|| Query || Manipulate ''Base'' Queries. || Range || Create, modify, apply ''Calc'' Cell Ranges.
|-
|| Report || Manipulate ''Base'' Reports. || Shapes || Create, modify, and insert shapes in ''Writer''.
|-
|| Sheet || Manipulate ''Calc'' document sheets. || SQLStatement || Create, modify and execute SQL Statements in ''Base''.
|-
|| Table || Manipulate ''Base'' Tables and ''Writer'' Text-Tables.|| ||
|}
 
{| class="wikitable"
|+ Which files to include - Examples
|-
! Task !! Recommendation
|-
|| Manipulate an existing Writer document.<br>The script should add some text, modify some formatting. || #Include LibreOfficeWriter.au3
|-
|| Extract data from Calc and insert into a Writer document. || #Include LibreOfficeCalc.au3<br>#Include LibreOfficeWriter.au3
|-
|| Remove a Sheet from an existing Calc document. || #Include LibreOfficeCalc_Doc.au3<br>#Include LibreOfficeCalc_Sheet.au3
|-
|| Perform a Find and Replace operation in a Calc Document || #Include LibreOfficeCalc_Doc.au3<br>#Include LibreOfficeCalc_Helper.au3<br>#Include LibreOfficeCalc_Range.au3<br>'''Or simply''' #Include LibreOfficeCalc.au3
|-
|| I don't care. I want a single include file that makes sure all functions are available. || #Include LibreOffice.au3
|}
 
 
{| class="wikitable"
|+ Functions - Naming Scheme
|-
|| Functions Naming Scheme: || _LO<Module>_<Object><Function>
|-
|| Example: || _LOWriter_ParObjCopy ("Copies" data selected by the ViewCursor)
|-
|| Where to find: || LibreOfficeWriter_Par.au3
|-
|| Description: || <Module> and <Object> can be found in the tables above. <Function> describes the manipulation to be done (read, modify, copy ...)
|}
 
===Example Code===
On [[LibreOffice-Examples|this page]] you'll find code examples for the LibreOffice modules
Base, Calc, Writer and the Portable LibreOffice version.
 
===How LibreOffice Portable is Initialized===
Keeping in mind the information mentioned in the [[#Applications vs the ServiceManager|differences section]], particularly the difference between Microsoft Office using Applications, and LibreOffice using a ServiceManager for Document management. Also how this UDF has chosen to spare the user from having to create the ServiceManager themselves for this UDF, instead creating the ServiceManager in the background, and storing it as a static variable. The exception to this method is when working with LibreOffice Portable. Obviously, the behind the scenes method of using ObjCreate for creating the ServiceManager wont work with portable LibreOffice because the COM server is not registered in the registry, which AutoIt relies upon to create COM Objects.<br>
To work around this, and allow the user to work with Portable LibreOffice, this UDF has a single function found in the ''LibreOffice_Helper.au3'' file, named ''_LO_InitializePortable''. This function is used to point the UDF to the location of the Portable LibreOffice folder. Depending on whether there is an existing installation of LibreOffice or OpenOffice, one of two things occurs:
 
# If there currently is not an installed version of either LibreOffice or OpenOffice, and if the path called is valid, a temporary registry entry is added to the HKEY_CURRENT_USER Registry key (See ''__LO_SetPortableServiceManager'' for all keys created) mirroring the installed version's entries to allow AutoIt to create an instance of the ServiceManager. Once a ServiceManager has been created, the entries are deleted.  
# If there is already an installed version of LibreOffice or OpenOffice, the ServiceManager is created using the already existing entry, and the registry remains unmodified.The reason this still works for the Portable version will be described next.  
 
From this point, whether the ServiceManager was created using the Portable version or the installed version, the portable instance of LibreOffice found at the called path is started invisibly using some special flags (See __LO_SetPortableServiceManager), causing it to begin listening on a certain port and localhost IP Address. Certain languages, such as C++, and Java are able to create a local instance of the ServiceManager object, and using this local version, are able to connect to this local IP and Port to obtain the COM reference to the started Libreoffice Portable service. Because AutoIt is not able to do this, the above process has been adopted. Using the created ServiceManager, either from an existing install, or by temporarily registering the Portable Version of LibreOffice in the User Registry, I can now use that ServiceManager to connect to the running Portable Version of LibreOffice, and obtain the Portable LibreOffice ServiceManager object from that instance of LO. Once this is obtained, it is then stored as a static variable for future use, eliminating the need to re-create it each time a ServiceManager is needed.
 
==Troubleshooting==
===Debug your Script===
To debug your script, as with any AutoIt function, the first place to start is checking for @error, and @extended values after each function call. Each function in this UDF has error, extended and return values documented in the function header.
 
===@error codes===
Currently there are 6 types of errors:
* 1. '''Input errors'''. This code indicates that the user has input the wrong type of data, or an out-of-bound value.<br>Make sure to re-read the parameters and any appropriate remarks for what is expected.<br> ''Example:'' @error = 1 and @extended = 1 stands for: ''$sFilePath not a string, or file not found.''
 
* 2. '''Initialization errors'''. This code indicates that the UDF failed to create some Object or other necessary item for use in a certain function.<br>These types of errors are generally out of the user’s control, and may indicate something has changed with LibreOffice that has caused this UDF to no longer function appropriately.<br> ''Example:'' @error = 2 and @extended = 1 stands for: ''Failed to create ServiceManager Object.''
 
* 3. '''Processing errors'''. This code indicates that the UDF failed to perform some action.<br> Maybe something wrong with the UDF, or with LibreOffice. Double check that any input values are appropriate for what you are doing in the function that is failing.<br>''Example:'' @error = 3 and @extended = 1 stands for: ''No open Libre Office documents found.''


=====Excel vs Calc=====
* 4. '''Property Setting errors'''. This code indicates that the function failed to set the requested property values.<br> Which properties have failed will be indicated in the returned @extended value, as a Bit value, this means that to determine what property or properties failed to be set you will need to us BitAND and compare the @extended value with the values listed in the function header.<br>''Example:'' @error = 4 and @extended = 10 stands for: ''Error setting $bReadOnly (2) '''AND''' Error setting $bLoadAsTemplate (8).''
In ''MS Excel'' there are two types of data selections. One is called a "''Selection''", which represents what the user has currently selected with the cursor, be it a single cell or a range of cells.<br>The second is known as "''Range''", a Range is a group of cells, such as A2 to C4, or even a single cell. Excel treats these two very similarly, a selection is essentially the same as a Range, but is visually selected.<p>
Similarly, in ''LO Calc'' there are two types of data selections. There are "''Selections''", which is whatever the user has selected with the cursor, this can be text within a cell, a single cell, a range of cells (e.g. A1 to B3) or several ranges of cells (e.g. A1:C3 and D3:F5). <br>
The second type is also called a "''Range''"", this can be either a single cell, a group of cells, or even the entire sheet is considered a Range.


====Newlines====
* 5. '''Printer errors'''. This code indicates problems with printers.<br> Whether when there were no printers found when retrieving the currently available printer names, or whether there was an issue when setting printer related settings.<br>''Example:'' @error = 5 and @extended = 1 stands for: ''No default printer found.''
Another difference it would be good to briefly touch upon, is the different line endings used between MS Office and LibreOffice. Microsoft Office, of course, uses the standard '''Windows''' '''''CRLF''''' (Carriage Return, Line Feed) hard newline sequence (when you push Enter while editing a document). And a '''''LF''''' (LineFeed) for a soft newline (when you push Shift+Enter in a document).<br>
LibreOffice on the other hand uses the '''Unix''' style newline sequences, either '''''CR''''' (Carriage Return) or '''''LF''''' (Line Feed). The '''''CR''''' sequence is used for hard newlines, and the '''''LF''''' sequence is used for soft newlines. This generally won't be encountered nor cause much issue for the majority of tasks while automating LibreOffice. However it may become an issue when getting or inserting strings, particularly in Writer. For example, if I select several paragraphs and retrieve the text of those paragraphs for processing in AutoIt, and then replace those selected paragraphs with the text I processed in AutoIt, the resulting text will most likely not have the same amount of newlines, there would be a mixture of hard and soft newlines in the paragraph, where before there maybe was only hard newlines. The root cause for this seems to be due to the way strings are handled when retrieving them from LibreOffice, newlines use the platform default newline sequence when retrieving the string, instead of maintaining the current newline sequence used by LibreOffice, so when working on Windows, the '''CRLF''' is used for all hard newlines. This is generally only true of inserting and retrieving strings directly in the document, or any frames, textboxes, pr table cells in Writer. Caution should be exercised when inserting a string, to manually use the '''''CR''''' or '''''LF''''' sequences as needed when wanting to insert a hard or soft newline respectively.<br>
Presently this UDF attempts to perform a find and replace operation for '''CRLF''' newlines to convert them to '''CR''' whenever strings are being set that allow newlines, and that may be affected by this issue. It is still recommended that the user does this themselves to ensure they obtain expected results. Calc does not seem to be affected by this issue, as it appears to convert any '''CR''' or '''CRLF''' to '''LF'''.


====Formatting====
* 6. '''Version errors'''. This code indicates that the current version of LibreOffice is too low for a specific function or parameter.<br> Any version limitations are documented in the function or parameter description. Either do not use the function or parameter that requires a higher LibreOffice version, or upgrade to a newer version of LibreOffice. <br>''Example:'' @error = 6 and @extended = 1 stands for: ''Current Libre Office version less than 7.4.''
One more less important difference between MSOffice and LibreOffice is formatting. When formatting text in MS Office, usually the user uses what is called “Direct Formatting”, i.e., selecting some text and selecting Bold, to bolden the text, or italic, etc. Not that you can't and don't create and apply formatting styles, but primarily this is how MS Office intends the user to apply formatting and styling.<br>
In LibreOffice however, the way the user applies formatting is intended to be slightly different. This is not to say that the user cannot use “direct formatting”, they can, and many do, but it sometimes does not produce desirable results when attempts are made later on to change this or that styling, often unsightly text ends up being produced, perhaps wrong text sizing when the font was made bigger, or words still in an old font style etc.<br>
The way LibreOffice intends the user to apply formatting and styling is by the use of what is called Paragraph Styles and Character styles. Paragraph styles, of course, would be used to apply, for example, a certain font to an entire paragraph, they modify and apply their affects to the whole paragraph where they are applied, except when overridden by direct formatting. Character styles on the other hand, as their name implies, apply the styling only to the character(s) it is applied to, generally this is used to apply coloring, bold, underlining or italic to characters as needed, except where direct formatting is applied already.<br>
Given the fact that LibreOffice also supports direct formatting as MS Office does, this difference isn't necessarily important. But for the best experience, both manually working with LibreOffice, and while automating it with this UDF, it is recommended to use the styling approach instead, or at least keep these facts in mind to you know what to expect.


===Limitations===
===Catching COM Errors===
* The UDF works ONLY for the installed version of LibreOffice, not the portable version.
A further step in debugging scripts is to monitor for COM errors, which can be triggered if something fails on the LibreOffice side.<br>
* The UDF is written using the English version of LibreOffice, and may only work for the English version of LibreOffice installations.  
This UDF has user-COM error functions that allow the user to have a custom user function be called each time a COM error is encountered, or optionally, to either use MsgBox or ConsoleWrite to display the COM error details.<br>
* Many functions in this UDF may or may not work with OpenOffice, however some settings are definitely for LibreOffice only.
Instructions for this are found in each respective COM Error function. Each component has its own dedicated COM user error handler, e.g., LibreOfficeWriter has the ''_LOWriter_ComError_UserFunction'', and also LibreOffice.au3 has its own COM error handler, called: ''_LO_ComError_UserFunction''. <br> A COM error error handler can display the following information about a COM error: Number, Description, At line, Source, Error Description, though it will not always have all of the values filled in.
* For those using older AutoIt versions, several functions use Maps. In older AutoIt installations, a syntax error will be reported due to this.
<br>If no ComError_UserFunction has been defined by the Skript, LibreOffice does nothing but handle the COM error, so your script does not crash and set @error and @extended.
 
Example output:
<blockquote>
! We intercepted a COM Error<br>
'''Number:''' 0x80020006<br>
'''Description:''' Unknown name.<br>
'''At line:''' 199<br>
'''Source:''' <br>
'''Error Description:''' <br>
</blockquote>
 
==FAQ==
There have been no frequently asked questions so far.
 
==Tools==
To be defined.


==References==
==References==
* [https://wiki.documentfoundation.org/Documentation/DevGuide/Glossary LibreOffice Developer's Guide: Glossary]
* [https://wiki.documentfoundation.org/Documentation/DevGuide/Glossary LibreOffice Developer's Guide: Glossary]
* [https://api.libreoffice.org/docs/idl/ref/index.html LibreOffice 24.2 SDK API Reference]
* [https://api.libreoffice.org/docs/idl/ref/index.html LibreOffice 24.2 SDK API Reference]

Latest revision as of 15:24, 25 February 2026

This article uses material from the Wikipedia article "LibreOffice" and other Wikipedia articles, which are released under the Creative Commons Attribution-Share-Alike License 4.0.

Introduction

This UDF (AU3LibreOffice) aids the user in creating and manipulating LibreOffice Documents.
This wiki article tries to explain basic principles of the UDF and offers tips on how to get started with automating LibreOffice. It also points out some differences between automating the Microsoft Office and LibreOffice suites.
It is not intended to explain LibreOffice's API.
To work with this UDF, you should be familiar with both AutoIt and LibreOffice.

What is LibreOffice?

LibreOffice is a free and open-source office productivity software suite developed by The Document Foundation (TDF).
It was created in 2010 as a fork of OpenOffice.org, itself a successor to StarOffice.
The suite includes applications for

  • word processing (Writer),
  • spreadsheets (Calc),
  • presentations (Impress),
  • vector graphics (Draw),
  • database management (Base), and
  • formula editing (Math).

It supports the OpenDocument format and is compatible with other major formats, including those used by Microsoft Office.
Generally speaking, OpenOffice and LibreOffice have the same foundational features, but given the fact that LibreOffice is more actively developed, LibreOffice has more, and newer features available.

Feature comparison: LibreOffice vs Microsoft Office

This page compares the features of LibreOffice and Microsoft Office in several form factors.
The comparisons separate major and minor feature differences and also includes notes on LibreOffice extensions.
The comparison highlights differences and therefore does not display any features which are present in both office suites.

LibreOffice UDF

Since LibreOffice is a fork of OpenOffice, this UDF also works with OpenOffice in many places. However, you should not rely on this, as this UDF was developed with a focus on LibreOffice.
All functions for which a minimum version of LibreOffice is specified are most likely NOT compatible with OpenOffice.

LibreOffice Object Model

The LibreOffice UDF is a wrapper for the complex LibreOffice object model (details can be found on this wiki page). It hides the complexity of the object model and replaces it with a few AutoIt function calls.
The functions provided by the UDF cover most of the functionality that is typically required. However, if you need more complex functions, you will have to write them yourself.
First, you need to familiarize yourself with the LibreOffice API.
The following books will be a valuable help also in this line.

Technical comparison: LibreOffice vs Microsoft Office

For those users familiar with Microsoft Office and its automation API, we have included some comparisons between the two suites' APIs and internal behaviour on the following page.

Requirements

Currently there are no external requirements needed to run this UDF. You are able to work with both the Installed and also the Portable version of LibreOffice. However do take note of the methods followed to work with Portable LibreOffice to make sure it will fit your requirements.

Limitations

  • The ability to automate LibreOffice Portable (and OpenOffice) has been added, and should work correctly. However to achieve this, it currently adds some temporary Registry entries to HKEY_CURRENT_USER, unless there is an installed version of LibreOffice or OpenOffice present. See _LO_InitializePortable for more detail.
  • This UDF has been written using the English version of LibreOffice, and some aspects may only work for the English version of LibreOffice installations without errors.
  • Many functions in this UDF may or may not work with OpenOffice, and some are definitely for LibreOffice only. These unfortunately have not been documented.
  • For those using AutoIt versions older than 3.3.16.1 some functions in this UDF use Maps, which will not be recognized as proper syntax in older AutoIt versions.

Special Terminology

Some terminology you will encounter frequently in the UDF, and sometimes in these articles, are the following (in alphabetical order):

Component
Any component or module of LibreOffice. They are: Base, Calc, Draw, Impress, Math, and Writer.
Hundredths of a MM (HMM)
As implied by the name itself, Hundredths of a MM is a hundredth part of a Millimeter, and is the measurement unit used by LibreOffice internally for nearly all widths, heights, distances, etc. For simplicity, this is the unit type used in this UDF as well, unless otherwise stated in the Parameters. A converter function has been supplied with this UDF, found in the “LibreOffice_Helper.au3" file, called _LO_UnitConvert. This function can convert from millimeters, centimeters or inches to HMM, and back again, depending on the flag called.
RGB color Integer
A color unit you will find frequently in this UDF. LibreOffice nearly always uses this type of color value when setting or retrieving a color property. The RGB color integer is a number from 0 to 16,777,215, where each separate number represents a color. The relationship between RGB and a RGB Integer is based on a simple calculation: RGB Integer = Blue x (256 x 256) + Green x 256 + Red, or in Hexadecimal, 0x00RRGGBB, where RR = Red component (0-255), GG = Green component (0-255), BB = Blue component (0-255), and the highest byte (alpha) is unused and usually 0.
Style Names
LibreOffice uses Styles for various things, such as Paragraph or Character styling. LibreOffice automatically translates these style names according to the user's default language currently set, these are called the “Display Name”. To enable Macros, as well as LibreOffice itself to work on any language system these style names are also internally named so that they can remain constant. In the English version of LibreOffice the Display Name and the Internal name, or “Programmatic Name” of every style is in most cases the same. There are a handful of exceptions to this however, for example “Default Paragraph Style” is internally named “Standard”, “No Character Style” is also internally called “Standard”, (but is classed as a Character Style, not a Paragraph Style).

To aid the user in recognizing what Styles are which, each respective style name retrieval function (e.g. _LOWriter_ParStylesGetNames) offers the ability to still retrieve the Display name, which will be returned in the same order as the Internal names are returned, allowing the user to easily have a list of each name and see which belong to which. On top of this, each respective style name retrieval function also lists any styles that have names differing from their internal names in the Remarks section of the header.

UI
User Interface, the visual windows and buttons and tabs the user sees and is used to work with LibreOffice manually.

Installation

Place the entire set of .au3 files of this UDF into a directory searched by SciTE (details can be found in the AutoIt help file for keyword #include).
No further installation or files are required.

Getting started

With the exception of working with Portable LibreOffice (see the Portable LibreOffice section regarding this), the first step will always be to acquire a Document object.
Simply open an existing file, connect to an already opened document or create an entirely new document.
Once a Document object is acquired, you then go forward from there to accomplish the task you desire to do.

Example: (No error checking is integrated, as we want to give a quick impression of how easy it is to create an AutoIt script with the LibreOffice UDF.)

#include "..\LibreOfficeWriter.au3"
Global $oDoc, $oViewCursor
$oDoc = _LOWriter_DocCreate(True, False)                       ; Create a New, visible, Blank Libre Office Document.
$oViewCursor = _LOWriter_DocGetViewCursor($oDoc)               ; Retrieve the document view cursor to insert text with.
_LOWriter_DocInsertString($oDoc, $oViewCursor, "Hello World")  ; Insert some text at the ViewCursor.
_LOWriter_DocClose($oDoc, True)                                ; Close and Save the document as .odt document to the desktop

What do I do with all of these files?

The LibreOffice UDF consists of a large number of files. How do you know which to include in your script?
To begin with, the files are arranged in a semi-hierarchical form (3 levels: suite, module and object).

Each filename consists of the string "LibreOffice" the optional module name and the optional object name.
These optional parts of the file name (including description) are listed in the following tables.
To help you decide which files to include in your script, examples are provided in the last table.

Hierarchy of files
Level Naming Scheme Example filename What do you get with this example? Comment
suite level LibreOffice.au3 LibreOffice.au3 All functions of all modules of the LibreOffice suite. Use include files on this level only when you need ALL LibreOffice modules in your script.
module level LibreOffice<Module>.au3 LibreOfficeWriter.au3 All functions of the LibreOffice Writer module. That's the recommended approach for 99.99% of the scripts.
object level LibreOffice<Module><_Object>.au3 LibreOfficeWriter_Doc.au3 All functions related to a document object of the Writer module. Use this level if you need to process a single object of a LibreOffice module.
Valid Modules
Module Description Module Description
Base Database management program, similar to Microsoft Access. Calc Spreadsheet program, similar to Microsoft Excel.
Draw Vector graphics editor, similar to Microsoft Visio. Impress Presentation program, similar to Microsoft PowerPoint.
Math Formula editor for mathematical expressions. Writer Word processor program, similar to Microsoft Word.
Valid Objects
Object Description Object Description
Cell Manipulate cells in Calc and Writer. Char Manipulate character styles in Writer.
Comments Manipulate cell comments in Calc documents. Constants Constants for the respective module.
Cursor Perform cursor movements in Calc and Writer. Database Manipulate Base Databases.
DirectFormatting Manipulate Direct Character and Paragraph formatting in Writer. Doc Manipulate documents in Base, Calc and Writer.
Field Manipulate Calc and Writer document fields. FootEndNotes Manipulate Writer Foot/End notes.
Form Manipulate Base Forms and Writer Forms and Form Controls. Frame Manipulate Writer Frames and Frame Styles.
Helper Manipulate data for use in various functions of the UDF. Images Insert and modify Images in Writer.
Internal Internal functions not to be used by Scripters! Num Manipulate Writer Numbering Styles.
Page Manipulate Writer and Calc Page Styles. Par Manipulate Writer Paragraph Styles.
Query Manipulate Base Queries. Range Create, modify, apply Calc Cell Ranges.
Report Manipulate Base Reports. Shapes Create, modify, and insert shapes in Writer.
Sheet Manipulate Calc document sheets. SQLStatement Create, modify and execute SQL Statements in Base.
Table Manipulate Base Tables and Writer Text-Tables.
Which files to include - Examples
Task Recommendation
Manipulate an existing Writer document.
The script should add some text, modify some formatting.
#Include LibreOfficeWriter.au3
Extract data from Calc and insert into a Writer document. #Include LibreOfficeCalc.au3
#Include LibreOfficeWriter.au3
Remove a Sheet from an existing Calc document. #Include LibreOfficeCalc_Doc.au3
#Include LibreOfficeCalc_Sheet.au3
Perform a Find and Replace operation in a Calc Document #Include LibreOfficeCalc_Doc.au3
#Include LibreOfficeCalc_Helper.au3
#Include LibreOfficeCalc_Range.au3
Or simply #Include LibreOfficeCalc.au3
I don't care. I want a single include file that makes sure all functions are available. #Include LibreOffice.au3


Functions - Naming Scheme
Functions Naming Scheme: _LO<Module>_<Object><Function>
Example: _LOWriter_ParObjCopy ("Copies" data selected by the ViewCursor)
Where to find: LibreOfficeWriter_Par.au3
Description: <Module> and <Object> can be found in the tables above. <Function> describes the manipulation to be done (read, modify, copy ...)

Example Code

On this page you'll find code examples for the LibreOffice modules Base, Calc, Writer and the Portable LibreOffice version.

How LibreOffice Portable is Initialized

Keeping in mind the information mentioned in the differences section, particularly the difference between Microsoft Office using Applications, and LibreOffice using a ServiceManager for Document management. Also how this UDF has chosen to spare the user from having to create the ServiceManager themselves for this UDF, instead creating the ServiceManager in the background, and storing it as a static variable. The exception to this method is when working with LibreOffice Portable. Obviously, the behind the scenes method of using ObjCreate for creating the ServiceManager wont work with portable LibreOffice because the COM server is not registered in the registry, which AutoIt relies upon to create COM Objects.
To work around this, and allow the user to work with Portable LibreOffice, this UDF has a single function found in the LibreOffice_Helper.au3 file, named _LO_InitializePortable. This function is used to point the UDF to the location of the Portable LibreOffice folder. Depending on whether there is an existing installation of LibreOffice or OpenOffice, one of two things occurs:

  1. If there currently is not an installed version of either LibreOffice or OpenOffice, and if the path called is valid, a temporary registry entry is added to the HKEY_CURRENT_USER Registry key (See __LO_SetPortableServiceManager for all keys created) mirroring the installed version's entries to allow AutoIt to create an instance of the ServiceManager. Once a ServiceManager has been created, the entries are deleted.
  2. If there is already an installed version of LibreOffice or OpenOffice, the ServiceManager is created using the already existing entry, and the registry remains unmodified.The reason this still works for the Portable version will be described next.

From this point, whether the ServiceManager was created using the Portable version or the installed version, the portable instance of LibreOffice found at the called path is started invisibly using some special flags (See __LO_SetPortableServiceManager), causing it to begin listening on a certain port and localhost IP Address. Certain languages, such as C++, and Java are able to create a local instance of the ServiceManager object, and using this local version, are able to connect to this local IP and Port to obtain the COM reference to the started Libreoffice Portable service. Because AutoIt is not able to do this, the above process has been adopted. Using the created ServiceManager, either from an existing install, or by temporarily registering the Portable Version of LibreOffice in the User Registry, I can now use that ServiceManager to connect to the running Portable Version of LibreOffice, and obtain the Portable LibreOffice ServiceManager object from that instance of LO. Once this is obtained, it is then stored as a static variable for future use, eliminating the need to re-create it each time a ServiceManager is needed.

Troubleshooting

Debug your Script

To debug your script, as with any AutoIt function, the first place to start is checking for @error, and @extended values after each function call. Each function in this UDF has error, extended and return values documented in the function header.

@error codes

Currently there are 6 types of errors:

  • 1. Input errors. This code indicates that the user has input the wrong type of data, or an out-of-bound value.
    Make sure to re-read the parameters and any appropriate remarks for what is expected.
    Example: @error = 1 and @extended = 1 stands for: $sFilePath not a string, or file not found.
  • 2. Initialization errors. This code indicates that the UDF failed to create some Object or other necessary item for use in a certain function.
    These types of errors are generally out of the user’s control, and may indicate something has changed with LibreOffice that has caused this UDF to no longer function appropriately.
    Example: @error = 2 and @extended = 1 stands for: Failed to create ServiceManager Object.
  • 3. Processing errors. This code indicates that the UDF failed to perform some action.
    Maybe something wrong with the UDF, or with LibreOffice. Double check that any input values are appropriate for what you are doing in the function that is failing.
    Example: @error = 3 and @extended = 1 stands for: No open Libre Office documents found.
  • 4. Property Setting errors. This code indicates that the function failed to set the requested property values.
    Which properties have failed will be indicated in the returned @extended value, as a Bit value, this means that to determine what property or properties failed to be set you will need to us BitAND and compare the @extended value with the values listed in the function header.
    Example: @error = 4 and @extended = 10 stands for: Error setting $bReadOnly (2) AND Error setting $bLoadAsTemplate (8).
  • 5. Printer errors. This code indicates problems with printers.
    Whether when there were no printers found when retrieving the currently available printer names, or whether there was an issue when setting printer related settings.
    Example: @error = 5 and @extended = 1 stands for: No default printer found.
  • 6. Version errors. This code indicates that the current version of LibreOffice is too low for a specific function or parameter.
    Any version limitations are documented in the function or parameter description. Either do not use the function or parameter that requires a higher LibreOffice version, or upgrade to a newer version of LibreOffice.
    Example: @error = 6 and @extended = 1 stands for: Current Libre Office version less than 7.4.

Catching COM Errors

A further step in debugging scripts is to monitor for COM errors, which can be triggered if something fails on the LibreOffice side.
This UDF has user-COM error functions that allow the user to have a custom user function be called each time a COM error is encountered, or optionally, to either use MsgBox or ConsoleWrite to display the COM error details.
Instructions for this are found in each respective COM Error function. Each component has its own dedicated COM user error handler, e.g., LibreOfficeWriter has the _LOWriter_ComError_UserFunction, and also LibreOffice.au3 has its own COM error handler, called: _LO_ComError_UserFunction.
A COM error error handler can display the following information about a COM error: Number, Description, At line, Source, Error Description, though it will not always have all of the values filled in.
If no ComError_UserFunction has been defined by the Skript, LibreOffice does nothing but handle the COM error, so your script does not crash and set @error and @extended.

Example output:

! We intercepted a COM Error
Number: 0x80020006
Description: Unknown name.
At line: 199
Source:
Error Description:

FAQ

There have been no frequently asked questions so far.

Tools

To be defined.

References