Libreoffice-WIP: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
 
(25 intermediate revisions by the same user not shown)
Line 4: Line 4:
==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 Microsoft Office and LibreOffice 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 32:


===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 42:


===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.  


====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.  


=====Excel vs Calc=====
; RGB color Integer
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>
: 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.
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====
; Style Names
Another difference to briefly touch upon, is the different line endings used between Microsoft Office and LibreOffice. <p>
: 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 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. Further than this, as stated above, each respective style name retrieval function also lists any styles that have names differing from their internal names.
''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).<p>
''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. <p>
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. <br>
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.<br>  
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 if inserting and retrieving strings directly in the document, or any frames, textboxes, or 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.<p>
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.<p>
Calc does not seem to be affected by this issue, as it appears to convert any '''CR''' or '''CRLF''' to '''LF'''.


====Formatting====
; UI
One more less important difference between ''Microsoft Office'' and LibreOffice is formatting. When formatting text in Microsoft 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 Microsoft Office intends the user to apply formatting and styling.<p>
: User Interface, the visual windows and buttons and tabs the user sees and is used to work with LibreOffice manually.
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, 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 Paragraph 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 apply the styling only to selected character(s), except where direct formatting is applied already.<p>
Given the fact that LibreOffice also supports direct formatting as Microsoft 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.


<br> *****************************************
==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.


===Limitations===
==Getting started==
* The UDF works ONLY for the installed version of LibreOffice, not the portable version.
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>
* The UDF is written using the English version of LibreOffice, and may only work for the English version of LibreOffice installations.  
Simply open an existing file, connect to an already opened document or create an entirely new document.<br>
* Many functions in this UDF may or may not work with OpenOffice, however some settings are definitely for LibreOffice only.
Once a Document object is acquired, you then go forward from there to accomplish the task you desire to do.<br>
* For those using older AutoIt versions, several functions use Maps. In older AutoIt installations, a syntax error will be reported due to this.
''Example'':
<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, "Some text")  ; 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.
 
{| class="wikitable"
|+  File hierarchy
|-
! Level !! Example filename !! What do you get?
|-
| suite level || LibreOffice.au3|| All functions of all modules of the LibreOffice suite.
|-
| module level || LibreOfficeWriter.au3 || All functions of the LibreOffice Writer module.
|-
| object level  || LibreOfficeWriter_Doc.au3 || All functions related to a document object of the Writer module.
|}
 
 
Hierarchy of files
Naming Scheme of files and function names
 
<br> **************************************
 
This chart illustrates part of the hierarchy, the file “LibreOffice.au3” has Includes in it for all sub-components. (Note: Some components in the chart aren't supported yet.). If you include this file, you can use any of the functions in all other sub-component files, all those listed in the chart. However, if you don't want such a large number of files included in your Script. You can include any sub-component you may need, such as LibreOfficeCalc.au3, or LibreOfficeBase.au3. Whichever component(s) you intend to automate.<br>
Going a step further, consider this chart:
 
[[File:Chart_2.jpg|800px|||Sub level LibreOffice file]]
 
This chart considers only one component, in this case Writer. LibreOfficeWriter.au3 has many sub-files for the various tasks that can be done in Writer; Cell, Cursor, Doc, etc. More detail about these sub-files will be touched a little later. If you include LibreOfficeWriter.au3 in your script, you can use any of the functions in all the sub-files for Writer. You can identify a sub-file because the name will contain the Component's name, followed by an underscore, in this case, LibreOfficeWriter_*.au3., where “*” is a sub-file category of some type, i.e. Table, Doc or Page.<br>
Lastly, you also have the option to include in your script only some of the sub-files themselves. This may be a better approach depending on how much you need to do. For example, if you only need to Export a document, one could potentially only include LibreOfficeWriter_Doc.au3, which would include all the needed functions for the task, such as *_DocOpen, *_DocExport, etc.<br>
One small tip, you will never need to deal with the files named “*_Internal.au3”, i.e. LibreOfficeWriter_Internal.au3, they contain only functions needed by the various sub-files and are already included in the sub-files where needed.
 
===How Do You Find What is Needed?===
Now we have reached the more interesting part, and also the more difficult. Most likely you have a certain task you need to accomplish, but how do you know what files you need for the job? Part of this problem we solved above, you (should) know already what component(s) of LibreOffice you want to automate, be it Calc, Base, or Writer, etc.Thus you know if you'll need to include LibreOfficeCalc.au3, LibreOfficeBase.au3 or LibreOfficeWriter.au3.<br>
The next step is finding the right functions. This is admittedly the harder part to do, especially when you first begin with this UDF. We have attempted to logically sort functions according to their usage as best as possible. The naming of the sub-files is the first clue. Functions dealing with Document related actions, such as opening, closing, saving, maximize, minimize, visible/invisible, undo/redo, are found in the “*_Doc.au3” file. Taking Calc as an example, The LibreOfficeCalc_Sheet.au3 file has functions for adding or deleting Sheets from the document, setting and retrieving which Sheet is currently active, as well as retrieving the Object for the Sheets to use in further operations. There are also files for dealing with Ranges, as well as Cursors and Fields. This should help narrow down which file you will need to look through.<br>
Next, open each sub-file that seems to be related and look at the “; #CURRENT#” header near the top of the script. This will have a list of all the functions contained in the file. With each function we have tried to include in the name a hint of the corresponding tab or section in the LibreOffice UI, if applicable, as well as, of course, the attributes the function will affect or the action it will perform. For example when setting background color or background gradient in L.O., these settings are found under the “Area” tab in most UI pages. The corresponding functions are some of the following: _LOCalc_CommentAreaColor and _LOCalc_CommentAreaGradient. In this case the functions all have to do with modifying a Calc Comment, Area is the Tab name when setting some of these properties, (Background Color or Gradient).<br>
While these tips are generally correct, they aren't a solid rule yet. This UDF has attempted to adhere to this as best as possible, but LibreOffice has a wide variety of similar settings, not always located in identical settings. For example, for Cell background, there is no “Area” tab, thus the background color functions are simply _LOCalc_CellStyleBackColor and _LOCalc_CellBackColor.<br>
Once you find the function you need to use, always check the parameters section in the header, it will aid in indicatating where to find what you need to use to call each parameter, or what kind or where to obtain the kind of Object you need. As an example, consider the following Parameter entries from ''_LOCalc_CellUnderline'',<br>
<blockquote>$oCell              - [in/out] an object. A Cell Range or Cell object returned by a previous _LOCalc_RangeGetCellByName, _LOCalc_RangeGetCellByPosition, _LOCalc_RangeColumnGetObjByPosition, _LOCalc_RangeColumnGetObjByName, _LOcalc_RangeRowGetObjByPosition, _LOCalc_SheetGetObjByName, or _LOCalc_SheetGetActive function.</blockquote>
This contains a few important details. First: The parameter ''$oCell'' accepts either a Calc Cell Range Object (i.e. a range Object covering A1:B3), Or a single Cell Object (i.e. Cell Object for Cell C3). Then follows a list of functions that would return such Objects that would be acceptable, this list is not always exhaustive when there are many functions that would return a similar acceptable Object or value. However the Object or value will either be named the same, or a note will be in the remarks section of the header indicating what else would be acceptable. Another example from ''_LOCalc_CellUnderline'',<br>
<blockquote>$iUnderLineStyle    - [optional] an integer value (0-18). Default is Null. The Underline line style, see constants, $LOC_UNDERLINE_* as defined in LibreOfficeCalc_Constants.au3.</blockquote>
This parameter has a few different and important details. First you will see some numerical values inside of parenthesis, 0-18, I will explain these shortly, you will also see it mentions a certain Constant name, in this case “$LOC_UNDERLINE_*”, this means, of course, that this parameter accepts the Constants of this name as values, the constants will be found in the file LibreOfficeCalc_Constants.au3. If you went to the Constants file, you would see 19 separate Constants to choose from. The values inside of the parenthesis means that this parameter accepts any of those constants that have values between 0 and 18.<br>
<blockquote>$iULColor            - [optional] an integer value (-1-16777215). Default is Null. The color of the underline, set in Long integer format. Can be a custom value, or one of the constants, $LO_COLOR_* as defined in LibreOffice_Constants.au3. Set to $LO_COLOR_OFF(-1) for automatic color mode.</blockquote>
This parameter is similar, again some values are found in parenthesis, -1 to 16,777,215. However if you went to the constants indicated, you would see that they only have a few options to choose from, but not an exhaustive list ranging from -1 to 16,777,215. That is why the parameter description indicates you can choose any custom value between -1 and 16,777,215, you can make up your own value as desired as long as it falls between those two values. We will touch another Parameter from another function as a final example.<br>
<blockquote>$iDistance          - [optional] an integer value. Default is Null. The distance of the Shadow from the Comment box, set in 1/100th MM.</blockquote>
Considering this Parameter, you will see that there are no parenthesis in this one, this generally means there are no limits, or perhaps only a minimum. If there is a minimum, and it is not mentioned in the parameter, it can be found by looking at the documented Input errors in the header. In this case, $iDistance has the following Input error:<br>
<blockquote>@Error 1 @Extended 4 = $iDistance not an Integer, or less than 0.</blockquote>
That means $iDistance must be an integer, and cannot be less than 0. But there is seemingly no maximum. Or perhaps the maximum varied, which would be noted in the Remarks section, but generally it means it is not present.
 
===A Few More Notes===
Throughout this UDF you will encounter functions that are for setting properties, whether it is Styles, Tables, Cells, etc. Usually, you will see that nearly all the parameters will be optional, except for the required Object parameter(s). The optional parameters will have an assigned value of “Null”, which will tell you that, unless indicted otherwise, this function can be used to both set AND retrieve the current property values. You can confirm if this is the case by looking at the Remarks section of the header for a statement similar to the following:<br>
<blockquote>Call this function with only the required parameters (or by calling all other parameters with the Null keyword), to get the current settings.</blockquote>
If you call this function with all optional parameters to Null the return will be an array (except when there is only one optional parameter, in which case the return will be of what ever type the Parameter is, an Integer, a String etc.) If there is more than one optional parameter, the returned array will be a single dimension (unless indicated otherwise), with each element of the array in the order of the optional parameters. For example:<br>
<blockquote>_LOWriter_ParStyleBorderColor(ByRef $oParStyle, $iTop = Null, $iBottom = Null, $iLeft = Null, $iRight = Null)</blockquote>
In this function you have the non-optional parameter, ''$oParStyle'', and four optional parameters, ''$iTop'', ''$iBottom'', ''$iLeft'', and ''$iRight''. If I called this function with only a Paragraph Style Object, it would return the current Paragraph Style Border color values, the returned array would contain the following values: $aArray[0] = $iTop, $aArray[1] = $iBottom, $aArray[2] = $iLeft, and $aArray[3] = $iRight.<br>
Furthermore, any functions that have optional parameters with the “Null” value assigned to them, means that you can also skip any parameters you do not want to set a value for by calling “Null” in the position, nothing will be modified for that property.
 
===Some of the other files===
There are some files I have not yet touched upon in these explanations, which we will now examine. Each separate component has a file named *_Helper.au3, there is also a general file called LibreOffice_Helper.au3. These files contain functions that either don't fit under the other categories, or are purely for aiding in other work. For example there is in LibreOffice_Helper.au3 functions such as _LO_UnitConvert, and _LO_VersionGet. _LO_UnitConvert can be used for converting from common units of measurement to the measurement unit used by LibreOffice internally (100th MM). VersionGet is used to determine the current LibreOffice/OpenOffice version being automated. This is beneficial when certain functions or parameters are only available for certain L.O. versions (These limitations will be indicated in any applicable functions, either in the parameter, or the description of the function itself if the whole function is restricted to a certain version.).<br>
Taking a look at LibreOfficeWriter_Helper.au3, we find some functions like the following: ''_LOWriter_FontsGetNames'', and ''_LOWriter_SearchDescriptorCreate''. _LOWriter_FontsGetNames, obviously, retrieves an array of all available Fonts for use in the Document. _LOWriter_SearchDescriptorCreate creates a Search descriptor Object for usage in the Writer Find and Replace functions found in the LibreOfficeWriter_Doc.au3 file.
 
===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.
 
------------
 
==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. In order to obtain a Document Object, the beginning step is to either open an existing file by providing the file path to the appropriate function, for example an odt or docx file would be opened with Writer, or an ods or xlsx file would be opened by Calc, etc. Alternatively you could also connect to an already opened document instance using one of the “Connect” functions. Or, finally, you may just want to create an entirely new Document using the appropriate “Create” function. The actual process on how to do this will be discussed in more detail for each element in another section.<br>
Once a Document object is acquired, you then go forward from there to accomplish the task you desire to do. We will touch on this shortly, but first it is important to deal with the most probable question on any newcomer's mind,


==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 13:34, 21 February 2026

This page is still a work in progress.

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 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. Further than this, as stated above, each respective style name retrieval function also lists any styles that have names differing from their internal names.

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:

#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, "Some text")  ; 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.

File hierarchy
Level Example filename What do you get?
suite level LibreOffice.au3 All functions of all modules of the LibreOffice suite.
module level LibreOfficeWriter.au3 All functions of the LibreOffice Writer module.
object level LibreOfficeWriter_Doc.au3 All functions related to a document object of the Writer module.


Hierarchy of files Naming Scheme of files and function names


**************************************

This chart illustrates part of the hierarchy, the file “LibreOffice.au3” has Includes in it for all sub-components. (Note: Some components in the chart aren't supported yet.). If you include this file, you can use any of the functions in all other sub-component files, all those listed in the chart. However, if you don't want such a large number of files included in your Script. You can include any sub-component you may need, such as LibreOfficeCalc.au3, or LibreOfficeBase.au3. Whichever component(s) you intend to automate.
Going a step further, consider this chart:

Sub level LibreOffice file

This chart considers only one component, in this case Writer. LibreOfficeWriter.au3 has many sub-files for the various tasks that can be done in Writer; Cell, Cursor, Doc, etc. More detail about these sub-files will be touched a little later. If you include LibreOfficeWriter.au3 in your script, you can use any of the functions in all the sub-files for Writer. You can identify a sub-file because the name will contain the Component's name, followed by an underscore, in this case, LibreOfficeWriter_*.au3., where “*” is a sub-file category of some type, i.e. Table, Doc or Page.
Lastly, you also have the option to include in your script only some of the sub-files themselves. This may be a better approach depending on how much you need to do. For example, if you only need to Export a document, one could potentially only include LibreOfficeWriter_Doc.au3, which would include all the needed functions for the task, such as *_DocOpen, *_DocExport, etc.
One small tip, you will never need to deal with the files named “*_Internal.au3”, i.e. LibreOfficeWriter_Internal.au3, they contain only functions needed by the various sub-files and are already included in the sub-files where needed.

How Do You Find What is Needed?

Now we have reached the more interesting part, and also the more difficult. Most likely you have a certain task you need to accomplish, but how do you know what files you need for the job? Part of this problem we solved above, you (should) know already what component(s) of LibreOffice you want to automate, be it Calc, Base, or Writer, etc.Thus you know if you'll need to include LibreOfficeCalc.au3, LibreOfficeBase.au3 or LibreOfficeWriter.au3.
The next step is finding the right functions. This is admittedly the harder part to do, especially when you first begin with this UDF. We have attempted to logically sort functions according to their usage as best as possible. The naming of the sub-files is the first clue. Functions dealing with Document related actions, such as opening, closing, saving, maximize, minimize, visible/invisible, undo/redo, are found in the “*_Doc.au3” file. Taking Calc as an example, The LibreOfficeCalc_Sheet.au3 file has functions for adding or deleting Sheets from the document, setting and retrieving which Sheet is currently active, as well as retrieving the Object for the Sheets to use in further operations. There are also files for dealing with Ranges, as well as Cursors and Fields. This should help narrow down which file you will need to look through.
Next, open each sub-file that seems to be related and look at the “; #CURRENT#” header near the top of the script. This will have a list of all the functions contained in the file. With each function we have tried to include in the name a hint of the corresponding tab or section in the LibreOffice UI, if applicable, as well as, of course, the attributes the function will affect or the action it will perform. For example when setting background color or background gradient in L.O., these settings are found under the “Area” tab in most UI pages. The corresponding functions are some of the following: _LOCalc_CommentAreaColor and _LOCalc_CommentAreaGradient. In this case the functions all have to do with modifying a Calc Comment, Area is the Tab name when setting some of these properties, (Background Color or Gradient).
While these tips are generally correct, they aren't a solid rule yet. This UDF has attempted to adhere to this as best as possible, but LibreOffice has a wide variety of similar settings, not always located in identical settings. For example, for Cell background, there is no “Area” tab, thus the background color functions are simply _LOCalc_CellStyleBackColor and _LOCalc_CellBackColor.
Once you find the function you need to use, always check the parameters section in the header, it will aid in indicatating where to find what you need to use to call each parameter, or what kind or where to obtain the kind of Object you need. As an example, consider the following Parameter entries from _LOCalc_CellUnderline,

$oCell - [in/out] an object. A Cell Range or Cell object returned by a previous _LOCalc_RangeGetCellByName, _LOCalc_RangeGetCellByPosition, _LOCalc_RangeColumnGetObjByPosition, _LOCalc_RangeColumnGetObjByName, _LOcalc_RangeRowGetObjByPosition, _LOCalc_SheetGetObjByName, or _LOCalc_SheetGetActive function.

This contains a few important details. First: The parameter $oCell accepts either a Calc Cell Range Object (i.e. a range Object covering A1:B3), Or a single Cell Object (i.e. Cell Object for Cell C3). Then follows a list of functions that would return such Objects that would be acceptable, this list is not always exhaustive when there are many functions that would return a similar acceptable Object or value. However the Object or value will either be named the same, or a note will be in the remarks section of the header indicating what else would be acceptable. Another example from _LOCalc_CellUnderline,

$iUnderLineStyle - [optional] an integer value (0-18). Default is Null. The Underline line style, see constants, $LOC_UNDERLINE_* as defined in LibreOfficeCalc_Constants.au3.

This parameter has a few different and important details. First you will see some numerical values inside of parenthesis, 0-18, I will explain these shortly, you will also see it mentions a certain Constant name, in this case “$LOC_UNDERLINE_*”, this means, of course, that this parameter accepts the Constants of this name as values, the constants will be found in the file LibreOfficeCalc_Constants.au3. If you went to the Constants file, you would see 19 separate Constants to choose from. The values inside of the parenthesis means that this parameter accepts any of those constants that have values between 0 and 18.

$iULColor - [optional] an integer value (-1-16777215). Default is Null. The color of the underline, set in Long integer format. Can be a custom value, or one of the constants, $LO_COLOR_* as defined in LibreOffice_Constants.au3. Set to $LO_COLOR_OFF(-1) for automatic color mode.

This parameter is similar, again some values are found in parenthesis, -1 to 16,777,215. However if you went to the constants indicated, you would see that they only have a few options to choose from, but not an exhaustive list ranging from -1 to 16,777,215. That is why the parameter description indicates you can choose any custom value between -1 and 16,777,215, you can make up your own value as desired as long as it falls between those two values. We will touch another Parameter from another function as a final example.

$iDistance - [optional] an integer value. Default is Null. The distance of the Shadow from the Comment box, set in 1/100th MM.

Considering this Parameter, you will see that there are no parenthesis in this one, this generally means there are no limits, or perhaps only a minimum. If there is a minimum, and it is not mentioned in the parameter, it can be found by looking at the documented Input errors in the header. In this case, $iDistance has the following Input error:

@Error 1 @Extended 4 = $iDistance not an Integer, or less than 0.

That means $iDistance must be an integer, and cannot be less than 0. But there is seemingly no maximum. Or perhaps the maximum varied, which would be noted in the Remarks section, but generally it means it is not present.

A Few More Notes

Throughout this UDF you will encounter functions that are for setting properties, whether it is Styles, Tables, Cells, etc. Usually, you will see that nearly all the parameters will be optional, except for the required Object parameter(s). The optional parameters will have an assigned value of “Null”, which will tell you that, unless indicted otherwise, this function can be used to both set AND retrieve the current property values. You can confirm if this is the case by looking at the Remarks section of the header for a statement similar to the following:

Call this function with only the required parameters (or by calling all other parameters with the Null keyword), to get the current settings.

If you call this function with all optional parameters to Null the return will be an array (except when there is only one optional parameter, in which case the return will be of what ever type the Parameter is, an Integer, a String etc.) If there is more than one optional parameter, the returned array will be a single dimension (unless indicated otherwise), with each element of the array in the order of the optional parameters. For example:

_LOWriter_ParStyleBorderColor(ByRef $oParStyle, $iTop = Null, $iBottom = Null, $iLeft = Null, $iRight = Null)

In this function you have the non-optional parameter, $oParStyle, and four optional parameters, $iTop, $iBottom, $iLeft, and $iRight. If I called this function with only a Paragraph Style Object, it would return the current Paragraph Style Border color values, the returned array would contain the following values: $aArray[0] = $iTop, $aArray[1] = $iBottom, $aArray[2] = $iLeft, and $aArray[3] = $iRight.
Furthermore, any functions that have optional parameters with the “Null” value assigned to them, means that you can also skip any parameters you do not want to set a value for by calling “Null” in the position, nothing will be modified for that property.

Some of the other files

There are some files I have not yet touched upon in these explanations, which we will now examine. Each separate component has a file named *_Helper.au3, there is also a general file called LibreOffice_Helper.au3. These files contain functions that either don't fit under the other categories, or are purely for aiding in other work. For example there is in LibreOffice_Helper.au3 functions such as _LO_UnitConvert, and _LO_VersionGet. _LO_UnitConvert can be used for converting from common units of measurement to the measurement unit used by LibreOffice internally (100th MM). VersionGet is used to determine the current LibreOffice/OpenOffice version being automated. This is beneficial when certain functions or parameters are only available for certain L.O. versions (These limitations will be indicated in any applicable functions, either in the parameter, or the description of the function itself if the whole function is restricted to a certain version.).
Taking a look at LibreOfficeWriter_Helper.au3, we find some functions like the following: _LOWriter_FontsGetNames, and _LOWriter_SearchDescriptorCreate. _LOWriter_FontsGetNames, obviously, retrieves an array of all available Fonts for use in the Document. _LOWriter_SearchDescriptorCreate creates a Search descriptor Object for usage in the Writer Find and Replace functions found in the LibreOfficeWriter_Doc.au3 file.

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.


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. In order to obtain a Document Object, the beginning step is to either open an existing file by providing the file path to the appropriate function, for example an odt or docx file would be opened with Writer, or an ods or xlsx file would be opened by Calc, etc. Alternatively you could also connect to an already opened document instance using one of the “Connect” functions. Or, finally, you may just want to create an entirely new Document using the appropriate “Create” function. The actual process on how to do this will be discussed in more detail for each element in another section.
Once a Document object is acquired, you then go forward from there to accomplish the task you desire to do. We will touch on this shortly, but first it is important to deal with the most probable question on any newcomer's mind,

References