Newbie: Win32 COM problem

Paul Hemans darwin at nowhere.com
Wed Aug 25 14:33:21 CEST 2010


Simple class to wrap the xlwt module for COM access

pyXLS.py:

from xlwt import Workbook

class WrapXLS:
    _reg_clsid_ = "{c94df6f0-b001-11df-8d63-00e09103a9a0}"
    _reg_desc_ = "XLwt wrapper"
    _reg_progid_ = "PyXLS.Write"
    _public_methods_ = 
['createBook','createSheet','writeSheetCell','saveBook']
    # _public_attrs_ = ['book']

    def __init__(self):
        self.book = None

    def createBook(self):
        self.book = Workbook()

    def createSheet(self,sheetName):
        self.book.add_sheet(sheetName)

    def writeSheetCell(self, sheet, row, col, value, style=""):
        sheet = self.book.get_sheet(sheet)
        sheet.write(row,col,value,style)

    def saveBook(self,fileName):
        self.book.save(fileName)

if __name__=='__main__':
    import win32com.server.register
    win32com.server.register.UseCommandLine(WrapXLS)

It registers ok with --debug. Code executing within Foxpro (no comments 
pls):
oPyXLS = CREATEOBJECT("PyXLS.Write")
oPyXLS.createBook()
oPyXLS.createSheet("Sheet 1")    -- Error here

Output in Python Trace Collector (PythonWin):
...
in _GetIDsOfNames_ with '(u'createsheet',)' and '1033'

in _Invoke_ with 1001 1033 3 (u'Sheet 1',)
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\win32com\server\dispatcher.py", line 
47, in _Invoke_
    return self.policy._Invoke_(dispid, lcid, wFlags, args)
  File "C:\Python26\lib\site-packages\win32com\server\policy.py", line 277, 
in _Invoke_
    return self._invoke_(dispid, lcid, wFlags, args)
  File "C:\Python26\lib\site-packages\win32com\server\policy.py", line 282, 
in _invoke_
    return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None)
  File "C:\Python26\lib\site-packages\win32com\server\policy.py", line 585, 
in _invokeex_
    return func(*args)
  File "C:\development\PyXLS\pyXLS.py", line 13, in createSheet
    def createBook(self):
AttributeError: WrapXLS instance has no attribute '_book'
pythoncom error: Python error invoking COM method.

Can anyone help? 





More information about the Python-list mailing list