[Tutor] Scripting Calligra sheets with Python

Jim Byrnes jf_byrnes at comcast.net
Tue Mar 19 02:56:30 CET 2013


On 03/18/2013 07:54 PM, Dave Angel wrote:
> On 03/18/2013 12:18 PM, Jim Byrnes wrote:
>> I am trying to script Calligra Sheets (formerly KSpread) with python.  I
>> have gotten some of the included example scripts to run so I know python
>> scripting is running.
>>
>> I found the following snippet on their website:
>>
>> import KSpread
>> sheet = KSpread.view().sheet()
>> # swap text of B5 and C6
>> t1 = sheet.text("B5")
>> t2 = sheet.text(6,3)
>> sheet.setText("B5", t2)
>> sheet.setText(6, 3, t1)
>> # swap value of D7 and E8
>> v1 = sheet.value("D7")
>> v2 = sheet.value(8,5)
>> sheet.setValue("D7", v2)
>> sheet.setValue(8, 5, v1)
>>
>> Error: 'str' object has no attribute 'text'
>>   File
>> "file:///usr/share/kde4/apps/sheets/scripts/extensions/myswap.py", line
>> 4, in <module>
>>
>>
>> This error message appeared in a dialog box.
>>
>> I've taught myself some Python but I don't understand what is happening
>> here.  Shouldn't t1 be a sheet object?  What would cause it to be a str
>> instead?
>>
>
> Since somebody has censored the rest of the error traceback, we can't
> even tell what line is giving the error.  Assuming you know it's
>     t1 = sheet.text("85")

I think line wrapping obscured that it is line 4 t1 = sheet.text("B5").

> then it has nothing to do with the type of t1, but with the type of
> sheet.  If that's the case, then print out type(sheet) and see what it
> actually is.  Then consult the documentation for the
> KSpread.view().sheet() function and see what it's documented to return.
>

The docs say: Returns the KSpread::ViewAdaptor object in which the 
document is displayed.

I would like to follow your advice and print out type(sheet) but right 
now I don't know how.  This is all running inside Calligra sheets and so 
far the only thing I have gotten out if it is the error message.

Thanks for the suggestion, now I can be a little more focused trying to 
figure this out, which is why I asked here first.  I have since taken 
Hugo's advise and posted to the Calligra list also.

Regards,  Jim

>> Thanks,  Jim
>>
>>
>
>




More information about the Tutor mailing list