question of easyExcel (<<Python Programming on Win32>>)

Tommy Zong metalzong at 163.com
Mon Apr 16 22:59:44 EDT 2007


Hi,

 

class easyExcel:

  def __init__(self, filename=None):

    self.xlApp = Dispatch('Excel.Application')

    if filename:

      self.filename = filename

      self.xlBook = self.xlApp.Workbooks.Open(filename)

    else:

      self.xlBook = self.xlApp.Workbooks.Add()

      self.filename = ''

 

  def save(self, newfilename=None):

    if newfilename:

      self.filename = newfilename

      self.xlBook.SaveAs(newfilename)

    else:

      self.xlBook.Save()

 

  def close(self):

    self.xlBook.Close(SaveChanges=0)

    del self.xlApp

 

  def getSheets(self):

    return self.xlBook.Worksheets

 

  def getCell(self, sheet, row, col):

    sht = self.xlBook.Worksheets(sheet)

    return sht.Cells(row, col).Value

 

  def setCell(self, sheet, row, col, value, noerr=True):

    sht = self.xlBook.Worksheets(sheet)

    sht.Cells(row, col).Value = value

 

  def cpSheet(self, before):

    shts = self.xlBook.Worksheets

    shts(1).Copy(None,shts(1))

 

  def show(self, visible=True):

      self.xlApp.Visible = visible

 

easyExcel comes from <<Python Programming on Win32>>, it provides a easy way
to process excel files. 

 

However, I found a problem today - it works fine in single thread version
but can not work properly in multi-thread version - If I move excel file
operations to sub-thread, it will complain 'CoInitialize has not been
called'. I noticed the exception is thrown in
"Lib\site-packages\win32com\client\dynamic.py", I tried to add
"pythoncom.CoInitialize()" in line 78 and "pythoncom.CoUninitialize()" in
line 330, it will be ok. I have following questions and hope someone can
give me some comments. Thanks a lot.

 

1.       After doing such modifications, the excel file will be opened as
"read only" mode, how to avoid this?

2.       Is it necessary to add these two lines? Is there any mistake I
made?

 

Below is information of my python:

ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on

Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on
win32

 

- Tommy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070417/50a28670/attachment.html>


More information about the Python-list mailing list