473,406 Members | 2,705 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,406 software developers and data experts.

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.AddReference("Microsoft.Office.Interop.Word")
clr.AddReference("Microsoft.Office.Interop.Excel")
clr.AddReference("Microsoft.Office.Interop.PowerPo int")
clr.AddReference("Office")

from Microsoft.Office.Interop.Word import ApplicationClass as
WordApplication
from Microsoft.Office.Interop.Excel import ApplicationClass as
ExcelApplication
from Microsoft.Office.Interop.PowerPoint import ApplicationClass as
PowerPointApplication
from Microsoft.Office.Interop.Word import WdReplace
from Microsoft.Office.Interop.Excel import XlCellType
from Microsoft.Office.Interop.Excel import XlSearchDirection
from System.Type import Missing
from System import GC

missing = Missing
FileLocation = "C:\\test.xls"

ExcelApp = ExcelApplication()
workbook= None

workbook = ExcelApp.Workbooks.Open(FileLocation, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing)

workbook.Save()
workbook.Close(SaveChanges=0)

ExcelApp.Quit()

workbook = None
ExcelApp = None

GC.Collect()
GC.WaitForPendingFinalizers()
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.ActiveWorkbook.Save()
ExcelApp.ActiveWorkbook.Close(SaveChanges=0)
ExcelApp.Workbooks.Close()

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

System.Runtime.InteropServices.Marshal.ReleaseComO bject(workbook)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(excelApp)

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

del(workbook)
del(excelApp)

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.
Rgds
Liam

Jan 10 '07 #1
0 2714

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

Similar topics

5
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,...
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
0
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...
1
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...
8
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...
2
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...
15
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. ...
0
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...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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,...
0
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...

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.