Macros/ScriptForge/DocumentPropertiesAsDictionaryQuickWin
Appearance
< Macros | ScriptForge
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
How to show document properties as dictionary object
Authored by Jean-Pierre Ledure.
The Dictionary service extends the capabilities of the Basic language. In Python it subclasses the standard dict
object. The service is illustrated by the document properties of LibreOffice documents.

How to run it in BASIC
Create a new document.
- Open the Basic IDE
- Select the first available blank module
- Copy and paste the Basic code
- Run Main()
How to run it in Python
Create a new document.
- Run APSO
- Create a new module, call it 'Module1'
- Copy and paste the Python code below
- Save and run the Main() method
Code
REM "Document properties" are mapped onto a dictionary
REM Minimal required version: LibreOffice 7.6 (updated 25,8)
REM Used service(s) Dictionary, Document, FileSystem
Sub DocumentProperties()
' Show the API of the Basic Dictionary service
Dim ui As Object
Dim fs As Object
Dim doc As Object
Dim dict As Object
Dim key As String
Dim message As String
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
Set ui = CreateScriptService("UI")
Set fs = CreateScriptService("FileSystem")
' New document based on a system template
Set doc = ui.CreateDocument(templatefile := fs.BuildPath(fs.TemplatesFolder, "personal/CV.ott"))
With doc
' The updatable properties Subject, Title, Description, Keywords are accessible directly
.Subject = "My curriculum vitae"
.Title = "Application"
.Description = "(Line 1) Application" _
& Chr(10) & "(Line 2) for the job"
.Keywords = "LibreOffice,Basic,Python,ScriptForge"
' Load the dictionary and lists its items
dict = .DocumentProperties
message = ""
For Each key in dict.Keys
message = message & key & " = " & dict.Item(key) & Chr(10)
Next key
End With
MsgBox message
End Sub
# coding: utf-8
from __future__ import unicode_literals
from scriptforge import CreateScriptService
basic = CreateScriptService('Basic')
### "Document properties" are mapped onto a dictionary
### Minimal required version: LibreOffice 7.6 (updated 25,8)
### Used service(s) Dictionary, Document
def documentproperties():
# Show the API of the Basic Dictionary service
ui = CreateScriptService('UI')
fs = CreateScriptService('FileSystem')
# New document based on a system template
doc = ui.CreateDocument(templatefile = fs.BuildPath(fs.TemplatesFolder, 'personal/CV.ott'))
# The updatable properties Subject, Title, Description, Keywords are accessible directly
doc.Subject = 'My curriculum vitae'
doc.Title = 'Application'
doc.Description = '(Line 1) Application\n(Line 2) for the job'
doc.Keywords = 'LibreOffice,Basic,Python,ScriptForge'
# Load the dictionary and lists its items
dico = doc.DocumentProperties
message = ''
for key, item in dico.items():
message = message + key + ' = ' + str(item) + '\n'
basic.MsgBox(message)
g_exportedScripts = (documentproperties,)