Jump to content

Macros/ScriptForge/DocumentPropertiesAsDictionaryQuickWin

From The Document Foundation Wiki


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.

Dialog showing document properties as Python dictionary
Document properties and their values.

How to run it in BASIC

Create a new document.

  1. Open the Basic IDE
  2. Select the first available blank module
  3. Copy and paste the Basic code
  4. Run Main()

How to run it in Python

Create a new document.

  1. Run APSO
  2. Create a new module, call it 'Module1'
  3. Copy and paste the Python code below
  4. 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,)

See also