Create Documents from Templates Using Python in LibreOffice

Let’s start with the full code, then dive down

# Created by Gweno 24/10/2025
# For tutolibro.org
# Public Domain, feel free to copy, modify, use in your own scripts
# 
# email: gweno@tutolibro.org

# This needs to be run from any instance of LibreOffice already open

# Python modules to import for this script:
import uno
from com.sun.star.beans import PropertyValue

# UNO service definition for creating document
context = uno.getComponentContext()
service_manager = context.getServiceManager()
    
def create_instance(name, with_context=False):
    if with_context:
        instance = service_manager.createInstanceWithContext(name, context)
    else:
        instance = service_manager.createInstance(name)
    return instance

def new_document_from_template(*args):
    desktop = create_instance('com.sun.star.frame.Desktop', True)
    properties = (PropertyValue(Name='AsTemplate', Value=True),)
    # full path to template here
    path = 'file:///usr/lib/libreoffice/share/template/common/officorr/Modern_business_letter_serif.ott'
    doc = desktop.loadComponentFromURL(path, '_default', 0, properties)
    return doc

def main():
    """ Creates a new document from a LibreOffice Temple (*.ott)"""
    
    new_document_from_template()
    
    
if __name__ == "__main__":
    main()


First we need to import the uno library and the specific PropertyValue function

# Python modules to import for this script:
import uno
from com.sun.star.beans import PropertyValue

Then let’s set the couple of service definitions that we need:

# UNO service definition for creating document
context = uno.getComponentContext()
service_manager = context.getServiceManager()

We create 3 functions:

create_instance:

def create_instance(name, with_context=False):
    if with_context:
        instance = service_manager.createInstanceWithContext(name, context)
    else:
        instance = service_manager.createInstance(name)
    return instance

new_document_from_template:

def new_document_from_template(*args):
    desktop = create_instance('com.sun.star.frame.Desktop', True)
    properties = (PropertyValue(Name='AsTemplate', Value=True),)
    # full path to template here
    path = 'file:///usr/lib/libreoffice/share/template/common/officorr/Modern_business_letter_serif.ott'
    doc = desktop.loadComponentFromURL(path, '_default', 0, properties)
    return doc

This function creates the new document itself from an template example within the shared library of LibreOffice.
Note the hard coded path, starting with ‘file://’ followed by the actual path for the file.
You can use your own template file. It doesn’t need to be a file with a ‘. ott’ extension. You can also us a or a regular ‘.odt’ file.
The example below shows the path for a simple odt file in my Templates folder. The new document will be new untitled file, copy of ‘MyDocs.odt’.

path = 'file:///home/gweno/Templates/MyDoc.odt'

I do recommend to use a dedicated template file wit ‘.ott’ for clarity and to be able to use it with the build-in LibreOffice Template management along with Templates features.

Finally, the main function just calls new_document_from_template:

def main():
    """ Creates a new document from a LibreOffice Temple (*.ott)"""
    
    new_document_from_template()

To execute.

Save the full code in a file in your usual python library for LibreOffice scripts to be able to execute this script from any open instance of LibreOffice.

Leave a Comment

Your email address will not be published. Required fields are marked *