KOffice is an interated Office Suite consisting of several applications where KSpread is the scriptable spreadsheet program which provides both table-oriented sheets and support for complex mathematical formulas and statistics.
The KSpread Scripting Plugin implements a plugin to dynamic access the scripting functionality from within KSpread. The plugin realizes usage-scenarios like;
The whole KSpread Scripting Plugin, that does handle all things related to scripting for KSpread, consist of only a small hand full of files;
The goal was to limit the work needed on an application to have it full scriptable. To achieve this internaly Qt's introspection-functionality like signals, slots, properties, enums, QObject's and QMetaObject/QMetaType/etc. is used to deal with functionality at runtime. To get a more detailed overview you may like to take a look at my talk about scripting with Kross.
The Handbook is generated from the sourcecode using doxygen and KWord's Import Doxygen XML File python script.
The htmlexport.py Python script demonstrates how extensions could be used. The script exports the content of the current sheet to a HTML file.
Extensions are located in the "Tools->Scripts" menu and are either distributed with KSpread (so some default extensions like the htmlexport.py script are installed together with KSpread as part of it) or could be later added and configured on demand using the "Script Manager".
If the user clicks on the "Ok" button the export-process starts and a progress-dialog is displayed to offer visual feedback while the Python script reads the content from the current sheet and writes it to a HTML file.
Ideas for additional extensions users may like;
KSpread has a rich set of formula functions for nearly every purpose. KSpread implements the whole set of the OpenFormula specification and could be easy extended with additional formula functions written in a scripting language.
Python script demonstrates this by defining the KSpread formula function "SCRIPT_TEST1";
functest1 = self.kspread.function("SCRIPT_TEST1")
functest1.minparam = 1
functest1.maxparam = 1
functest1.comment = (
"The SCRIPT_TEST1() function demonstrates how to " "use scripting functions. All it does is to take " "a string as argument and return the same string."
functest1.syntax = "SCRIPT_TEST1(string)"
"The string that should be returned")
# just return the first argument functest1.result = "%s" % argument
Now we run KSpread with the --scriptfile argument that points to the delivered functions.py
chmod 755 `kde4-config --install data`/kspread/scripts/functions/functions.py
kspread --scriptfile `kde4-config --install data`/kspread/scripts/functions/functions.py
The screenshot below shows KSpread using the new formula function "SCRIPT_TEST1" which was added and is handled in the functions.py script.
All at the functions.py script added formula functions are accessible via "Insert=>Function..." in the "Scripts" category.
Ideas for additional formula functions users may like;
The consoledocker.py Python script does provide a interactive Python Console Docker to KSpread.
The script uses PyQt4 to embed a QTextBrowser and a QLineEditor as GUI-editor widgets.
import PyQt4.Qt as Qt
widget = Qt.QWidget() docker = sip.wrapinstance(KoScriptingDocker.__toPointer__(), Qt.QDockWidget) docker.setWidget(widget)
Following samples are implementations of use-case scenarios KSpread could be used for. The scripts are simple executable files that run from within the commandline by using the "kross" application which is installed together with the Kross scripting framework.
The following Python script reads the OpenDocument Spreadsheet file "/home/kde4/invoicetemplate.ods" and writes the first sheet to a CSV (Comma Separated Value) file.
The complete csvexport.py script including the invoicetemplate.ods OpenDocument Spreadsheet file used as template is available as csvexport.tar.gz. Download, extract, edit the csvexport.py to change the "filename" and "csvfile" variables to point to the correct locations and execute the csvexport.py Python script ("chmod 755 csvexport.py" to make the script executable and "./csvexport.py" to execute the script).
The csvexport.py Python script looks like;
filename = "/home/me/invoicetemplate.ods"
csvfile = "/home/me/invoicetemplate.csv"
import Kross kspread = Kross.module("kspread")
if not kspread.openUrl(filename):
raise "Failed to open %s" % filename
import csv csvwriter = csv.writer( open(csvfile,'w') )
sheet = kspread.sheetByName(kspread.sheetNames())
for row in range(1, sheet.lastRow() + 1):
# Put the content of the row into the record-list. record =  for col in range(sheet.lastColumn() + 1, 1, -1): value = sheet.text(col, row) if value or len(record) > 0: record.insert(0,value) # If the record has at least one cell print it. if len(record) > 0: csvwriter.writerow( record )
The htmlexport.py Python
script distributed with KSpread could also be executed from
chmod 755 `kde4-config --install data`/kspread/scripts/extensions/htmlexport.py
`kde4-config --install data`/kspread/scripts/extensions/htmlexport.py
The odfpyexport.py Python script distributed with KSpread uses the OdfPy Python module to write an OpenDocument text file (ODT) from KSpread. Take the script as example how easy scripting with KSpread is and modify it to your needs to produce custom OpenDocument files.
The following Python script reads the OpenDocument Spreadsheet template-file "/home/kde4/invoicetemplate.ods", fills some cells within a sheet and writes a new OpenDocument Spreadsheet file.
The complete invoice-example including the invoice.py Python script and the invoicetemplate.ods OpenDocument Spreadsheet file used as template is available as invoice.tar.gz. Download, extract, edit the invoice.py to change the "templatefile" and "savefile" variables to point to the correct locations and execute the invoice.py Python script ("chmod 755 invoice.py" to make the script executable and "./invoice.py" to execute the script).
The invoice.py Python script looks like;
templatefile = "/home/me/invoicetemplate.ods"
savefile = "/home/me/invoice.ods"
import Kross kspread = Kross.module("kspread") if not kspread:
raise "KSpread is not installed."
if not kspread.openUrl(templatefile):
raise "Failed to open %s" % templatefile
sheet = kspread.sheetByName(kspread.sheetNames())
sheet.setText(1,7,"Joe User") sheet.setText(1,8,"Userstreet. 1") sheet.setText(1,9,"Testcasecity")
if not kspread.saveUrl(savefile):
raise "Failed to save %s" % savefile
Zope is an open source application server for building content management systems, intranets, portals, and custom applications with a large community of hundreds of companies and thousands of developers all over the world. Zope is written in Python, a highly-productive, object-oriented scripting language and supports the XML-RPC and SOAP protocols.
The kspread2zope.tar.gz tarball includes a Python script that demonstrates how to connect KSpread with the Zope application server using XML-RPC. What the script does is to download a resource from the Zope server, then parses it into KSpread sheets, changes some content and then either save it as OpenDocument Spreadsheet file or upload the content at the Zope server.