471,310 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Python COM and Excel

I'm struggling to tame Excel using Python and COM.

Most of the following code is working except for the setting of the activesheet.

Also, I would like to know how to release a COM object without closing the application / dispatcher that launched excel?

Expand|Select|Wrap|Line Numbers
  1. class ExcelCom32(object):
  2.     """
  3.     Access Excel using python via COM.
  4.     """
  5.     def __init__(self, visible=False):
  6.         self.app = Dispatch("Excel.Application")
  7.         #self.app.Visible = 0
  8.         self.sheet = 1
  9.  
  10.     def openBook(self, filename):
  11.         """
  12.         Open an existing Excel workbook.
  13.         """
  14.         # Set the path to the current working directory and add the path to the TxDatabase
  15.         MyPath = os.getcwd() + r'\TxDatabase'        
  16.  
  17.         self.app.Workbooks.Open(MyPath + '\\' + filename)
  18.  
  19.     def setActiveSheet(self, sheet):
  20.         """
  21.         Set the active worksheet.
  22.         """
  23.         self.ActiveSheet = sheet
  24.  
  25.     def getRange(self, range):
  26.         """
  27.         Get a range object for the specified range or single cell.
  28.         """
  29.         return self.app.ActiveWorkbook.Sheets(self.sheet).Range(range)
  30.  
  31.     def getValue(self, x, y):
  32.         """
  33.         Get the value of a 'cell' using Cells(x, y).
  34.         """
  35.         value = self.app.ActiveSheet.Cells(y, x).Value
  36.         return value
  37.  
  38.     def setVisible(self, v):
  39.         """
  40.         Set the Excel Application to Visible
  41.         """
  42.         self.app.Visible = v
  43.  
  44.     def setValue(self, v, x, y):
  45.         """
  46.         Get the value of a 'cell' using Cells(x, y).
  47.         """
  48.         self.app.ActiveSheet.Cells(y, x).Value = v
  49.  
  50.     def closeBook(self):
  51.         """
  52.         Close the active workbook.
  53.         """
  54.         self.app.ActiveWorkbook.Close()
  55.  
  56.     def quit(self):
  57.         """
  58.         Quit Excel.
  59.         """
  60.         return self.app.Quit()
  61.  
  62.     def findColEnd(self, x, y):
  63.         """
  64.         Find the last value in the spreadsheet and set the end point
  65.         """
  66.         c = 0
  67.         for yy in range(y, 1000):
  68.             value = self.app.ActiveSheet.Cells(yy, x).Value
  69.             if value == None:
  70.                 break
  71.             c = c + 1
  72.         return c + y
  73.  
  74.     def findRowEnd(self, x, y):
  75.         """
  76.         Find the last value in the spreadsheet and set the end point
  77.         """
  78.         c = 0
  79.         for xx in range(x, 1000):
  80.             value = self.app.ActiveSheet.Cells(y, xx).Value
  81.             if value == None:
  82.                 break
  83.             c = c + 1
  84.         return c + x
Sep 13 '07 #1
0 1853

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by Allison Bailey | last post: by
reply views Thread by justin worrall | last post: by
2 posts views Thread by Kevin T. Ryan | last post: by
3 posts views Thread by Xiao-Qin Xia | last post: by
10 posts views Thread by sam | last post: by
6 posts views Thread by ɽ | last post: by
4 posts views Thread by michael.pearmain | last post: by
6 posts views Thread by AleydisGP | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.