xlwings issue writing to cells in Excel 2010

Viewed 1339

At present I'm working to manage SQL connections to an Excel file via a Python script. The script uses xlwings to read and write to the workbook. Unfortunately, only the 'read' piece is functioning. I've combed Stackoverflow for quite some time today and noone seems to have this exact issue. Here is some debugging code I wrote up in the pertinent Python script(it exists on a worksheet as a UDF, the sheet I'm trying to write to is a different one); I'm not terribly experienced with Python or xlwings yet so feel free to gripe about structure/syntax :)

@xw.func
def compatTest():
    #use active workbook
    wb = xw.Book.caller()
    #does the sheet Notes exist?
    if wb.sheets['Notes']:
        status = "Status: "
        #does A1 in Notes have a value?
        if wb.sheets['Notes'].range('$A$1').value:
            status += "Good"
            #Can we write to the specified empty cell?
            try: 
                wb.sheets['Notes'].range('$E$1').value = 'something'
            #Nope :(
            except Exception as e:
                status = e.message
                return status + "can't write :("
            #This return statement shouldn't evaluate, just casting a wide net here
            return status + " " + str(wb.sheets['Notes'].range('$A$1').value)
        else:
            #A1 in notes is blank
            status += "Bad"
            wb.sheets['Notes'].range('$E$1').value = 'nothing'
            return status + " " + 'blank :('

Output in cell with the UDF in it: "can't write :(" - this means the error message isn't being returned.

I'm using the xlwings 0.11.4 add-in and VBA reference(I am not using the VBA module as it appears to overlap with the add-in and generates VBA error 49 'Bad DLL calling convention'). I haven't touched the add-in code, as unlike the module the settings aren't handled in the file. The settings I have entered in the xlwings ribbon are all accurate.

Current settings:
OS: Windows Server 2008 R2 Enterprise(testing here due to restrictions, script won't be deployed here)
Python version/distro: 2.7/Anaconda2
xlwings version: 0.11.4
Excel version: 2010 v. 14.0.7182.5000(32 bit)


Edit: Interestingly enough, when I first open the file(after updating links & hitting 'Continue' for outdated links), the cell with the UDF displays "Status: Good ". If I double click the cell to re-evaluate the UDF, it changes to "can't write :(" again.
If I change the except block to the following:

 except:
            a = str(sys.exc_info()[0])
            b = str(sys.exc_info()[1])
            c = str(sys.exc_info()[2])
            status = str(a + " \ " + b + " \ " + c)
            return status

I get:

"<class 'pywintypes.com_error'> \ (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None) \ <traceback object at 0x0000000007B95708>"


Edit #2:
Oddly enough I can write to cells using the xw.sub decorator:

@xw.sub
def SomeFunction():
    wb = xw.Book.caller()
    wb.sheets['Notes'].range('B3').value = "hello world"

That code works just fine, but I'd rather run as much as I can through UDFs if possible.

1 Answers
Related