473,583 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Excel With Python

I'm very new to Python, well IronPython to precise, and have been
having problems when using Excel.

The problem I'm having is the closing of my Excel object. I'm able to
successfully quit the Excel Application that I create, but when I open
a Workbook in the Application I can't successfully Quit Excel (by this
I mean I can quit it, but the Excel process isn't getting killed and I
have to manually go this through Task Manager).

I've given a sample of code below to hopefully make things clearer.
I've then given all of the information I think might be useful (sorry
if I've gone over the top!).
import System
import clr
from System.Data import *
from System.Web import *
from System.Web.UI import *
from clr import *

clr.AddReferenc e("Microsoft.Of fice.Interop.Wo rd")
clr.AddReferenc e("Microsoft.Of fice.Interop.Ex cel")
clr.AddReferenc e("Microsoft.Of fice.Interop.Po werPoint")
clr.AddReferenc e("Office")

from Microsoft.Offic e.Interop.Word import ApplicationClas s as
from Microsoft.Offic e.Interop.Excel import ApplicationClas s as
ExcelApplicatio n
from Microsoft.Offic e.Interop.Power Point import ApplicationClas s as
PowerPointAppli cation
from Microsoft.Offic e.Interop.Word import WdReplace
from Microsoft.Offic e.Interop.Excel import XlCellType
from Microsoft.Offic e.Interop.Excel import XlSearchDirecti on
from System.Type import Missing
from System import GC

missing = Missing
FileLocation = "C:\\test.x ls"

ExcelApp = ExcelApplicatio n()
workbook= None

workbook = ExcelApp.Workbo oks.Open(FileLo cation, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing)

workbook.Close( SaveChanges=0)


workbook = None
ExcelApp = None

GC.WaitForPendi ngFinalizers()
I've simpilised the code by taking all of my Workbook processing from
it and the problem is still occurring. As I said above if I don't
create the workbook, then ExcelApp closes as expected (there are no
stray processes).

I've read many articles and postings over the last few days regarding
this, but have had no luck with anything I've seen, examples of this
are now given.

I've tried adding the below code, but with no luck.

ExcelApp.Active Workbook.Save()
ExcelApp.Active Workbook.Close( SaveChanges=0)
ExcelApp.Workbo oks.Close()

I've also tried the below (again with no luck).

System.Runtime. InteropServices .Marshal.Releas eComObject(work book)
System.Runtime. InteropServices .Marshal.Releas eComObject(exce lApp)

And, I've also tried the following (again with no luck!).


It may seem strange using the Garbage Collector (well I wouldn't have
thought about using it here), but it was something that I read about
using for code written in C#. I've tried the code in C# (what I
normally write in) and all works fine, the only real difference is that
I'm setting the objects to NULL in C# and None here in IronPython -
Does this make a difference, is there something else I should be
setting it to? The Workbook object HAS to be set to NULL in C#, it's
then picked up by the Garbage Collector and the task disappears from
the Task Manager.

I did think of killing the actual EXCEL.EXE process at the end of my
code, but there might be several versions of the containing application
running on the same box, so I can't kill all of the Excel processes.

As I said above, I'm sorry if I've gone over the top in my description.
Any ideas or pointers would be greatly appreciated as I'm now going
round in circles.

Thanks in advance.

Jan 10 '07 #1
0 2730

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Bram Stolk | last post by:
pyHello, Google helped me find a nice package to interface python with MS Excel. "win32all", or sometimes called by the name "pywin32". http://starship.python.net/crew/mhammond/ However, this win32all extension seems to be specifically built for the windows platform only.
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...
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
by: liam_jones | last post by:
I'm very new to Python, well IronPython to precise, and have been having problems when using Excel. The problem I'm having is the closing of my Excel object. I'm able to successfully quit the Excel Application that I create, but when I open a Workbook in the Application I can't successfully Quit Excel (by this I mean I can quit it, but the...
by: Girish | last post by:
Hi, I want to embed a txt document into an excel using python. Here is my code, but i get an error message =================================================== Traceback (most recent call last): File "C:\Documents and Settings\kusumap\Desktop\Girish.py", line 7, in ? worksheet.OLEObjects.Add(Filename="C:\Documents and Settings
by: fahadqureshi | last post by:
Hello, I posted here before asking a question about reading excel. Now that the reading problem is solved i am having trouble on how to write to an excel file. This was my code to read the excel file: from win32com.client import Dispatch xlApp = Dispatch ("Excel.Application") xlWb = xlApp.Workbooks.Open ("c:/fahad/new/file.xls") xlSht...
by: Krishna | last post by:
I was trying to delete rows in an existing .xls file using python. How do I do that? I was using the following code, it seem to work if I type in python window, but if I save it in text editor and drage and drop the .py file, it doesnt work. What am I doing wrong here? Thanks for your help! import win32com.client from time import sleep...
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. http://www.mscibarra.com/webapp/indexperf/excel?priceLevel=0&scope=0&currency=15&style=C&size=36&market=1897&asOf=Jul+25%2C+2008&export=Excel_IEIPerfRegional Want to write python...
by: Albert-jan Roskam | last post by:
Hi John, Thanks! Using a higher xlrd version did the trick! Regarding your other remarks: -yep, input files with multiple sheets don't work yet. I kinda repressed that ;-) Spss outputs only single-sheet xls files, but I agree it's nicer if the programs works in other cases too. -and no, I don't intend to use data fields. Wouldn't it be easier...
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...
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...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
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...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.