Intro

With the 2.0 release the scripts Application Programming Interface (API) is unsupported and unfinished and known to change. Use the API at your own risk! When the API is stable you can depend on future versions not breaking your scripts, this support is not present in the 2.0 release of KWord.

The Scripting Plugin

The KWord Scripting Plugin implements a plugin to dynamic access the scripting functionality from within KWord.

The KWScriptingPart class implements a KPart component to integrate scripting as plugin into KWord.

The Scripting::Module class enables access to the KWord functionality from within the scripting backends.

The FrameSet and Frame classes are holding the content that is displayed on screen.

The TextDocument class represents a QTextDocument within the Scribe text-engine KWord uses to enable editing of text content. The TextCursor implements a control structure for the successive traversal of content within such a TextDocument .

Scripting Handbook

The KWord Scripting Handbook contains a full reference of the functionality accessible from within the scripting backends.

KoStore and XML

The following python sample does demonstrate how to print the element-names of all xml-files that are within the KWord KoStore backend (see KoScriptingOdf.h) for our current document.

importKWordstore=KWord.store()reader=store.open("META-INF/manifest.xml")ifnotreader:raise"Failed to read the mainfest"paths=[]foriinrange(reader.count()):ifreader.type(i)=="text/xml":paths.append(reader.path(i))forpinpaths:reader=store.open(p)ifnotreader:raise"Failed to read %s"%pdefonElement():printreader.name()reader.connect("onElement()",onElement)reader.start()

Such a combined doxygen XML file can then be imported into our running KWord instance by running "Import Doxygen XML File" python script from the Tools/Scripts-menu.

Import File with OpenOffice.org UNO

The python script ooimport.py uses the PyUNO module to access OpenOffice.org and import content from any by OpenOffice.org supported fileformat.
For this an optional hidden OpenOffice.org instance need to be started. Then the script connects as client to this OpenOffice.org server instance and controls it. If the script got executed and the connecting to the server failed, then it will startup such a hidden OpenOffice.org server instance and shuts it down again once the work is done.

If the script got executed from within KWord (e.g. by using the "Tools=>Script Manager") the following dialog is displayed. The user chooses the file he likes to import and may also able to change settings related to how the connection to OpenOffice.org should be established. Then a progressdialog will be displayed while in the background we try to connect with OpenOffice.org and let it load the file and pass the content back to KWord.

That way KWord is able to optional use the OpenOffice.org import-filters to import content from file formats like;

Export to a Text or HTML File

The python script exportfile.py allows to export content to text- or html-files.

XML Viewer

The python script xmlviewer.py to view the ISO OpenDocumentText XML representation of the current document displayed in KWord.

The XML Viewer does also allow to open the XML of the current document with an external application like KWrite or KXMLEditor. The "Compare..." button enables to compare the current XML with the XML of another ODT file.