def save_as(workbook, path): """[workbook].save_as(['path'])""" uri = GOffice.shell_arg_to_uri(path) workbook_view = Gnm.WorkbookView.new(workbook) file_saver = GOffice.FileSaver.for_file_name(uri) command_context = Gnm.CmdContextStderr.new() Gnm.wb_view_save_as(workbook_view, file_saver, uri, command_context) return True
def gnm_init(): global cc global ioc # Initialize Gnm itself Gnm.init() # create a stderr reporting context cc = Gnm.CmdContextStderr.new() # Load plugins Gnm.plugins_init(cc) # A context for io operations ioc = GOffice.IOContext.new(cc) return ()
#!/usr/bin/python # ----------------------------------------------------------------------------- import gi gi.require_version('Gnm', '1.12') from gi.repository import Gnm Gnm.init() # A context for reporting errors to stderr cc = Gnm.CmdContextStderr.new() # Load plugins Gnm.plugins_init(cc) # ----------------------------------------------------------------------------- # Create a workbook with one sheet wb = Gnm.Workbook.new_with_sheets(1) # Get sheet. Index starts at 0 sheet = wb.sheet_by_index(0) print("Name: {}".format(sheet.props.name)) print("Number of columns: {}".format (sheet.props.columns)) print("Number of rows: {}".format (sheet.props.rows)) # Store values and expressions is some cells. Coordinates are (col,row) # both starting at 0. (So what the gui sees as row 1 is 0 here.) sheet.cell_set_value(0,0,Gnm.Value.new_int(10)) sheet.cell_set_value(0,1,Gnm.Value.new_float(101.25)) sheet.cell_set_text(0,2,"=A1+A2") sheet.cell_set_text(0,3,"'01")
#!/usr/bin/python # ----------------------------------------------------------------------------- import gi gi.require_version('Gnm', '1.12') from gi.repository import Gnm from gi.repository import GOffice as Go Gnm.init() if Gnm.in_tree: print("Using in-tree gi.overrides.Gnm") else: print("Using installed gi.overrides.Gnm at {}".format( gi.overrides.Gnm.__file__)) # ----------------------------------------------------------------------------- print("\nTesting GnmValue overrides:") # __str__ print(Gnm.Value.new_empty()) print(Gnm.Value.new_bool(0)) print(Gnm.Value.new_bool(1)) print(Gnm.Value.new_int(12)) print(Gnm.Value.new_float(12.5)) print(Gnm.Value.new_string("howdy")) v = Gnm.Value.new_float(12.5) v.set_fmt(Go.Format.new_from_XL("0.00")) print(v) # -----------------------------------------------------------------------------
#!/usr/bin/python3 -B # ----------------------------------------------------------------------------- import GnumericTest import gi gi.require_version('Gnm', '1.12') gi.require_version('GOffice', '0.10') from gi.repository import Gnm from gi.repository import GOffice Gnm.init() import sys src_uri = GOffice.filename_to_uri(sys.argv[1]) dst_uri = GOffice.filename_to_uri(sys.argv[2]) # A context for reporting errors to stderr cc = Gnm.CmdContextStderr() # Load plugins Gnm.plugins_init(cc) # A context for io operations ioc = GOffice.IOContext.new(cc) # Read a file wbv = Gnm.WorkbookView.new_from_uri(src_uri, None, ioc, None) wb = wbv.props.workbook print("Loaded {}".format(wb.props.uri)) # Save a file
#!/usr/bin/python # ----------------------------------------------------------------------------- import gi gi.require_version('Gnm', '1.12') gi.require_version('GOffice', '0.10') from gi.repository import Gnm from gi.repository import GOffice Gnm.init() # A context for reporting errors to stderr cc = Gnm.CmdContextStderr.new() # Load plugins Gnm.plugins_init(cc) # ----------------------------------------------------------------------------- print("Savers available:") for fs in GOffice.get_file_savers(): print("Saver ID: {}".format(fs.props.id)) print(" Descripton: {}".format(fs.props.description)) print(" Mime type: {}".format(fs.props.mime_type)) print(" Extension: {}".format(fs.props.extension)) print(" Overwrite: {}".format(fs.props.overwrite)) print(" Interactive-only: {}".format(fs.props.interactive_only)) print(" Format level: {}".format(fs.props.format_level)) print("")
""" Gnumeric interface for the IAC protocol Requirements: - Install gnumeric - Enable the "Python plugin loader" by going to Tools > Plug-ins... -> Plugin List and selecting it from the checkbox list. """ from gi.repository import GOffice from gi.repository import Gnm import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") Gnm.init() class CellRangeCalculator(object): def column_index(self, column): if len(column) == 1: column_list = list(column[0]) else: column_list = column base26 = [] for i in range(len(column_list)): if column_list[i].isdigit(): return False else: base26.append(ord(column_list[i].lower()) - ord("a") + 1) return base26
#!/usr/bin/python # ----------------------------------------------------------------------------- import gi gi.require_version('Gnm', '1.12') gi.require_version('GOffice', '0.10') from gi.repository import Gnm from gi.repository import GOffice Gnm.init() import sys src_uri = GOffice.filename_to_uri (sys.argv[1]) dst_uri = GOffice.filename_to_uri (sys.argv[2]) # A context for reporting errors to stderr cc = Gnm.CmdContextStderr.new() # Load plugins Gnm.plugins_init(cc) # A context for io operations ioc = GOffice.IOContext.new (cc) # Read a file wbv = Gnm.WorkbookView.new_from_uri (src_uri, None, ioc, None) wb = wbv.props.workbook print("Loaded {}".format(wb.props.uri)) # Save a file fs = GOffice.FileSaver.for_file_name (dst_uri) if wbv.save_as (fs, dst_uri, cc):
#!/usr/bin/python3 -B # ----------------------------------------------------------------------------- import GnumericTest import gi gi.require_version('Gnm', '1.12') from gi.repository import Gnm Gnm.init() print(Gnm.qnorm(0.4, 0, 1, 1, 0))
#!/usr/bin/python # ----------------------------------------------------------------------------- import gi gi.require_version('Gnm', '1.12') from gi.repository import Gnm Gnm.init() print(Gnm.qnorm(0.4,0,1,1,0))
def main(): gnm_init() # Open (or create if it doesn't exist) our price tracking workbook. wb = wb_open("btcprices.gnumeric") # Select the first sheet in the workbook, check the name, and update name. sheet = wb.sheet_by_index(0) if sheet.props.name != "Bitcoin Prices": sheet.props.name = "Bitcoin Prices" # Check to see if there is a title row by checking if there's any value set at B2. # If none is set, populate the title row and style it. This is effectively a # run-once operation. if sheet.cell_get_value(1, 1) is None: # Labels sheet.cell_set_text(1, 1, "Timestamp (UTC)") sheet.cell_set_text(2, 1, "BTC Price (USD)") # Bold st = Gnm.Style.new() st.set_font_bold(1) r = Gnm.Range() # col row col row range, so 1:1 (B2) r.init(1, 1, 2, 1) sheet.apply_style(r, st) # Adjust Column Width # TBD - no blessed direct sizing interface, using autofit #r = Gnm.Range() #r.init(0,0,4,4) #Gnm.colrow_autofit_col(sheet, r) # Find first unused row in sheet to set up the addition of data for row in range(2, sheet.props.rows): if sheet.cell_get_value(1, row) is None: break # At this point, row points to the next open row # We will use the json interface to coinbase at https://api.coindesk.com/v1/bpi/currentprice.json # to extract the updated time and the current USD value, and then populate the row with that data. # Note that the user-agent needs to be set to a sane value, as urllib's default is banned due to abuse. # (per coinbase's request in their json feed, please don't abuse the interface with excessive requests!) # import urllib2, json url = "https://api.coindesk.com/v1/bpi/currentprice.json" agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11" opener = urllib2.build_opener() opener.addheaders = [('User-Agent', agent)] response = opener.open(url) data = json.loads(response.read()) # Update the row with the time the price was updated # we're getting the time in ISO 8601 format, which gnumeric won't directly # interpret, so we're going to convert it to python time, and the re-write it # in yyyy-mm-dd hh:mm:ss format that gnumeric is fine with. import dateutil.parser import time tdate = dateutil.parser.parse(data['time']['updated']) # Note we use set_text and not set_value(Gnm.Value.new_string()) because we # want gnumeric to interpret our input for us into a time value. If we just # wanted the raw text, we'd use set value and new_string. sheet.cell_set_text(1, row, tdate.strftime("%Y-%m-%d %H:%M:%S")) # ... then we update with the current USD exchange rate. # We get a little fancy using "locale" to deal with comma separators in the # ascii to floating point conversion using atof. (We could just set text in # gnumeric as well, but then we wouldn't be showing off setting # floating point values ;-) import locale locale.setlocale(locale.LC_NUMERIC, '') sheet.cell_set_value( 2, row, Gnm.Value.new_float(locale.atof(data['bpi']['USD']['rate']))) # Let's format the price to just dollars with a thousands separator st = Gnm.Style.new() st.set_format_text("$0,000") r = Gnm.Range() r.init(2, row, 2, row) sheet.apply_style(r, st) # Whew! All done. Save it out and quit. wb_save(wb) # Cleanup wb = None ioc = None quit()