473,389 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 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 1997

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

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
12
by: rhmd | last post by:
Just found Python and I love it. What an elegant language! I would like to use it for various applications, but the mathematical calculations are way too slow (a million sines 8 seconds in Python...
0
by: justin worrall | last post by:
Hi, I have a very simple python win32 example (gleaned from the O'Reilly book on Win32 programming for Python), of an Excel client with a COM server. Essentially I have three VB buttons -...
2
by: Kevin T. Ryan | last post by:
Hi Group - I have written a "semi-program" in MS Excel related to running a football pool. I've updated it over the past two years or so, to the point where it is getting pretty advanced. ...
3
by: Xiao-Qin Xia | last post by:
I don't like VBA, and want to use python to work with Excel. Does anybody recommend some good documents to begin with? Many thanks, Xiao-Qin
10
by: sam | last post by:
Hi group, I m wondering which Excel module is good to be used by Python? Thanks Sam
5
by: mbbx6spp | last post by:
Hi All, I already searched this newsgroup and google groups to see if I could find a Python equivalent to Perl's Template::Extract, but didn't find anything leading to a Python module that had...
6
by: ɽ | last post by:
i want to compare the content in excel,but i don't know whick module to use! can you help me?
4
by: michael.pearmain | last post by:
Hi Experts, Looking for a very quick bit on of advice on how to make some python code run. I'm a newbie to both VBA and Python, so i apologise if this is very easy but i'm about to tear my hair...
6
by: AleydisGP | last post by:
I have a .plt file (which is a tab delimited ASCII file) and I want to format it to get a .dbf with data in rows and columns, detele some rows/columns and substitute decimal '.' with ','. All this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.