473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python COM and Excel

1 New Member
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 2013

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

Similar topics

13
35549
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 and extract information from specific worksheets and cells. I'm not really sure how to get started with this process. I ran the COM Makepy utility from my PythonWin (IDE from ActiveSTate),
12
3137
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 vs. 2 seconds in Excel VBA), so was thinking of learning enough C++ to do the number crunching in C++ and integrating the C++ routines with Python. My question: My question: Which compiler works best with Python? I use Windows XP and 98. Have...
0
1652
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 - start_server, stop_server and test_server. The first two do the obvious, the last takes inputs from two cells in the excel sheets, then passes them to the python server which adds them together and returns the result an Excel message dialog.  
2
3283
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. Only, there are a few tricks that I want to add that I could not do in Excel - so I did them in Python :) Except as it stands, the current python scripts have to be run via the command line. Does anyone know how to allow interaction between Excel and...
3
3438
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
5883
by: sam | last post by:
Hi group, I m wondering which Excel module is good to be used by Python? Thanks Sam
5
2064
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 similar functionality. I am a big fan of Python as an OO language and use it for many system admin utilities, webdev and even MS Excel AddIn development, but I have found that Perl's Template::Extract is so invaluable to my web scraping utilities...
6
2478
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
7949
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 out after googling for the last 3 days. I have written a large python script which inside of it creates an Excel table, the name of this file and how many objects can change for each project i run.
6
6666
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 using Python (I'm using Pythonwin). The .plt file looks like this: * ISCST3 (02035): Tersa * MODELING OPTIONS USED: * CONC URBAN ELEV DFAULT
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.