def automation_example(rows, cols, value): """copies value to a range of rows x cols below the calling cell""" # Get the address of the calling cell using xlfCaller caller = pyxll.xlfCaller() address = caller.address # The update is done asynchronously so as not to block Excel by # updating the worksheet from a worksheet function def update_func(): # Get the Excel.Application COM object xl = xl_app() # Get an Excel.Range object from the XLCell instance range = caller.to_range(com_package="win32com") # get the cell below and expand it to rows x cols range = xl.Range(range.Resize(2, 1), range.Resize(rows + 1, cols)) # and set the range's value range.Value = value # kick off the asynchronous call the update function pyxll.async_call(update_func) return address
def ml_zoo_predict(tree, features): # Convert the features dictionary into a DataFrame with a single row features = pd.DataFrame([features], columns=tree._feature_names) # Get the prediction from the model prediction = tree.predict(features)[0] # Update the image in Excel async_call(show_image_in_excel, prediction) return _zoo_classifications[prediction]
def create_temporary_file(suffix=None): """Create a named temporary file that is deleted automatically.""" # Rather than delete the file when it's closed we delete it when the # windows message loop next runs. This gives Excel enough time to # load the image before the file disappears. file = tempfile.NamedTemporaryFile(suffix=suffix, delete=False) def try_delete(filename): try: os.unlink(filename) except PermissionError: # retry if Excel is still accessing it async_call(os.unlink, filename) # Make sure the file gets deleted after the function is complete async_call(try_delete, file.name) return file
def automation_example(rows, cols, value): """copies value to a range of rows x cols below the calling cell""" # get the address of the calling cell using xlfCaller caller = pyxll.xlfCaller() address = caller.address # the update is done asynchronously so as not to block some # versions of Excel by updating the worksheet from a worksheet function def update_func(): xl = xl_app() range = xl.Range(address) # get the cell below and expand it to rows x cols range = xl.Range(range.Resize(2, 1), range.Resize(rows+1, cols)) # and set the range's value range.Value = value # kick off the asynchronous call the update function pyxll.async_call(update_func) return address
elif x.ndim == 2: range = xl.Range(range.Resize(2, 1), range.Resize(x.shape[0]+1, x.shape[1])) y = x else: raise ValueError('Array rank must be 1 or 2.') range.Value = y except Exception, ex: logger.info(ex) # #======================================================================= # kick off the asynchronous call to the update function pyxll.async_call(update_func, arr) #============================================================================== def draw_table(tbl): """ Renders a table = list of rows = list of lists. We assume the CALLER did the proper type conversions!!! (We can handle strings, int32, and float64 colums.) """ if not isinstance(tbl, list): raise TypeError('List expected.')
def try_delete(filename): try: os.unlink(filename) except PermissionError: # retry if Excel is still accessing it async_call(os.unlink, filename)