[no subject]



 

Gnumeric and Python (or other scripting languages).


  • From: Jean Brefort <jean brefort normalesup org>
  • To: Gnumeric List <gnumeric-list gnome org>
  • Subject: Gnumeric and Python (or other scripting languages).
  • Date: Thu, 16 Aug 2012 18:50:17 +0200

Hi,

Next Gnumeric will support introspection, and this changes the way
Python and gnumeric can interact. Although the Gnumeric Python interface
is still there, it is better to use introspection. In the Python
console, if you type (with a fresh new workbook):

>>> from gi.repository import Gnm
>>> wb=Gnm.App.workbook_get_by_index(0)
>>> sheet=wb.sheet_by_index(0)
>>> cell=sheet.cell_create(0,0)
>>> cell.set_text("Hello World!")

A1 is now populated, but the view is not updated. Selecting the Gnumeric
window will show it. It is also possible to do that using Python, but it
is much more difficult.

You can also do things from a script or using Python in the terminal:

>>> from gi.repository import GOffice
>>> from gi.repository import Gnm
>>> Gnm.init()
>>> wb=Gnm.Workbook.new_with_sheets(1)
>>> sheet=wb.sheet_by_index(0)
>>> cell=sheet.cell_create(0,0)
>>> cell.set_text("Hello World!")
>>> uri=GOffice.shell_arg_to_uri("hello-world.gnumeric")
>>> wbv=Gnm.WorkbookView.new(wb)
>>> fs=GOffice.FileSaver.for_file_name(uri)
>>> cc=Gnm.CmdContextStderr.new()
>>> Gnm.wb_view_save_as(wbv,fs,uri,cc)
True
>>> quit()

As can be seen there are some (actually many) issues with namespaces in
gnumeric code. If everything was correct, Gnm.wb_view_save_as(wbv,...)
would be wbv.save_as(...).

Things should work with other languages fof which introspection bndings
exist (see https://live.gnome.org/GObjectIntrospection/Users).

As I'm all but n Python (or any other scripting language) expert, I
didn't test, and there might be bugs around.

Regards,
Jean




Hi Jean,

I'm not a programmer just and engineer,
so I couldn't do it myself even if I wanted to, but this looks so much like your examples...
please check http://sourceforge.net/projects/pyworkbooks/ to add some awesome friendly functionality with great performance and "msoffice" compatibility to the "python API", for example:

(the following text is not formatted so please check the pdf)

Summary/ Cheat-sheet:
Data Reading:
data = "" # return data point
data = "" 0:20] # returns array of your data type
data = "" # returns generator for a matrix of data
data = "" 0:40:6] # stepping/splicing. Don't use in Excel
data = "" (0,5,8)] # select specific points to retrieve (advanced stepping)
data = "" 0:] # INVALID INPUT. Length needs to be specified
Data Writing:
B[0,0] = 4
# single point
B[0, :20] = 44 # copying 44 over a row. Splice specifies length
B[:20, 0] = 44 # copying 44 over a column. Splice specifies length
B[0, 0:] = 44 # INVALID INPUT. Length needs to be specified
B[1, 0:] = range(100)
# writes out an array. Note that the splice only specifies the
# direction (does not specify the length), and can be left empty
B[0,0] = [[n*p for n in range(100)] for p in range(100)]
# writing a matrix
* Same addressing features as Data Reading.
Referencing:
B.change_cellref(0,5)
# makes it so that calling B[0,0] will return the value at 0,5
# recommended that you do not use if you use string addressing
B.change_sheet('Sheet1') # sheet addressing with string
B.change_sheet(1) # sheet addressing with index
B.current_sheet_name # stores current sheet name. Do not change
B.change_workbook('Book1.xlsx') # workbook addressing, string or int (use string)
B.current_workbook_name # current workbook name. Do not change
B.working_directory
# variable stores current workbook's working directory. Only for
# Gnumeric right now
Sheet Addressing in Call:
data = "" 'Sheet3'] # gets the data from Sheet3 without needing to change sheet
Creating/Renaming Sheets:
B.create_sheet('sheetname')
B.rename_sheet('oldsheetname', 'newsheetname')
Data Types:
B.change_dtype(list)
# change dtype to list
B.change_dtype(tuple)
B.change_dtype(int) # change dtype to array of numpy integers
B.change_dtype(float) # change dtype to array of numpy floating point values
Sheet Write Protection:
B.protect_sheet('Sheet1')
# protects a sheet. Not tested across workbooks. May be buggy
B.unprotect_sheet('Sheet1')
More Advanced (and probably useless) features:
B.ALWAYS_GENERATE = True
# arrays will be returned as generator-like objects
# (matricies will still be generators of arrays)

Thank you very much!
fdo

Attachment: PyWorkbooks cheat-sheet.pdf
Description: Adobe PDF document



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]