[Tutor] Scripting Calligra sheets with Python

Jim Byrnes jf_byrnes at comcast.net
Thu Mar 21 01:26:54 CET 2013


On 03/19/2013 09:16 PM, Jim Byrnes wrote:
> On 03/18/2013 11:25 PM, Dave Angel wrote:
>> On 03/18/2013 09:56 PM, Jim Byrnes wrote:
>>> 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").
>>
>> I saw the line 4, but didn't see anyplace where it showed me line 4.  So
>> i had to guess.  And for all I knew, the error isn't happening on that
>> line, but on some line called indirectly by that one.  The full stack
>> trace would be reassuring.  But apparently your Calligra environment is
>> censoring it.
>>
>>>
>>>> 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.
>>
>> Are you permitted to edit this myswap.py file?  If so, add a line that
>> prints the useful information immediately before the line which causes
>> the exception.  And if print is also swallowed by your helpful
>> environment, then write to a file.
>
> Yes I can and it does swallow it, so I redirected to a file and it says
> <type 'str'>.  Same as the error message.  So now I need to figure out
> why it's not returning the object expected.
>
> I can run the sample scripts that were installed but not something I
> originate.  I guess I must be missing something procedural that is
> keeping them from running properly.
>
> Thanks for you help.
>
> Regards,  Jim
>
>

Just a follow up for completeness in case some else finds this and has 
the same problem.

Replace line 2   sheet = KSpread.view().sheet()

with

sheetname = KSpread.currentSheet().sheetName()
sheet = KSpread.sheetByName(sheetname)

Then it will run with no errors.

Regards,  Jim






More information about the Tutor mailing list