[python-win32] python-win32 Digest, Vol 101, Issue 24

The Little Guy the_little_guy at gmx.com
Wed Aug 24 03:56:45 CEST 2011


Mr. Roberts,

 

It's, working, now, with the suggestion from Mr. Robinow.  It hasn't failed 

at this point.  But to answer your question, it was failing, every single 

time depending on how I modified the code.  I was about to give up on it. 

Now EXCEL.EXE goes away from Windows Task Manager, which happened only 

50% of the time.

 

It appears, that, indeed, xlrd was hogging up the file.  That was, really, 

unexpected as I had read information on xlrd, suggesting that would not 

happen.  Maybe, I did not do, enough, research on it, before using it.

 

Here's, the fully, working script.  Please keep in mind that, it's got bugs,


no error checking as of yet, as I was concentrating on getting the file to 

save without complaining.  

 

Thanks for all the help.  It is appretiated :-)

Little Guy

 

The code: (if you test it, make sure to change the working_dir path)

---------------------------------------------------------------------------

#!/usr/bin/env python

 

# shell utility module to work with os

import shutil

# Third party library allows us to open Excel files, find last row.

from xlrd import open_workbook

# Allows us to use MakeTime(date_object)

import pythoncom

# Allows to connect to Windows object using COM objects.

import win32com.client

# Allows us to work with date, time objects.

from datetime import date, time, datetime

# Allows us to work with time and os functions.

import os, sys, time

 

# Set the Excel file name, working directory and path

old_file_name = 'simple.xls'

working_dir = r"C:\Users\TestUser\Documents\Python" + os.sep

old_file_path = os.path.join(working_dir, old_file_name)

 

#-----------------------------xlrd
section--------------------------------------

# Open the Excel file for reading

book = open_workbook(old_file_path, on_demand=True)

sheet = book.sheet_by_name('Current')

lst_row = sheet.nrows

print lst_row

book.release_resources()

#-------------------------Today's date
section----------------------------------

# Returns date object (yyyy-mm-dd)

today = date.today()

print today

# Return date/time object (mm/dd/yy hh:mm:ss)

date_today = pythoncom.MakeTime(today)

print date_today

 

#---------------------------------------------------------------------------
----

 

# The win32com function to open Excel.

xlApp = win32com.client.Dispatch("Excel.Application")

xlApp.Visible = True

 

# Open the file we want in Excel

workbook = xlApp.Workbooks.Open(old_file_path)

 

# Extract some of the file's components we may need.

workbook = xlApp.ActiveWorkbook

xlApp.Sheets('Current').Select()

activesheet = xlApp.ActiveSheet

activesheet.Cells(lst_row + 1, 1).Value = "Time Object"  # Text data for now

activesheet.Cells(lst_row + 1, 2).Value = date_today

 

save_file = int(raw_input("Save the data Yes = 1, No = 2? "))

try:

    if save_file == 1:

##        workbook.Saved = 0

        workbook.Save()

##        workbook.Close(SaveChanges=True)

        xlApp.Quit()

    else:

        print 'File not saved!'

        workbook.Close()

        xlApp.Quit()

except KeyboardInterrupt:

    print 'Error: '

pythoncom.CoUninitialize()

----------------------------------------------------------------------------
---

 

-----Original Message-----
From: python-win32-bounces+the_little_guy=gmx.com at python.org
[mailto:python-win32-bounces+the_little_guy=gmx.com at python.org] On Behalf Of
python-win32-request at python.org
Sent: Tuesday, August 23, 2011 6:34 PM
To: python-win32 at python.org
Subject: python-win32 Digest, Vol 101, Issue 24

 

Send python-win32 mailing list submissions to

    python-win32 at python.org

 

To subscribe or unsubscribe via the World Wide Web, visit

    http://mail.python.org/mailman/listinfo/python-win32

or, via email, send a message with subject or body 'help' to

    python-win32-request at python.org

 

You can reach the person managing the list at

    python-win32-owner at python.org

 

When replying, please edit your Subject line so it is more specific

than "Re: Contents of python-win32 digest..."

 

 

Today's Topics:

 

   1. Re: Opening, Modifying, and Saving an Excel File from Python?

      (Tim Roberts) (Tim Roberts)

   2. Re: Opening, Modifying,  and Saving an Excel File from Python?

      (The Little Guy)

   3. Re: Opening, Modifying, and Saving an Excel File from Python?

      (Tim Roberts) [SEC=PERSONAL] (Andrew MacIntyre)

 

 

----------------------------------------------------------------------

 

Message: 1

Date: Tue, 23 Aug 2011 18:18:33 -0700

From: Tim Roberts <timr at probo.com>

To: Python-Win32 List <python-win32 at python.org>

Subject: Re: [python-win32] Opening, Modifying, and Saving an Excel

    File from Python? (Tim Roberts)

Message-ID: <4E545169.8070209 at probo.com>

Content-Type: text/plain; charset="windows-1252"

 

The Little Guy wrote:

> 

> Mr. Roberts,

> 

>  

> 

> I?ve actually, gone through the, routine of placing quit in multiple

> places but still I get errors.  Either an unknown process error is

> generated or another instance of Excel pops up, with a SaveAs dialog,

> box, with a random filename as default.  Something has grabbed the

> excel file.

> 

 

Can you post the whole script as you have it now?  How many times to you

have to run it before it fails?

 

-- 

Tim Roberts, timr at probo.com

Providenza & Boekelheide, Inc.

 

 

 

------------------------------

 

Message: 2

Date: Tue, 23 Aug 2011 18:33:34 -0700

From: "The Little Guy" <the_little_guy at gmx.com>

To: "'David Robinow'" <drobinow at gmail.com>

Cc: python-win32 at python.org

Subject: Re: [python-win32] Opening, Modifying, and Saving an Excel

    File from Python?

Message-ID: <5DAE6EBACB59467E90DD27657EE2BC3D at BLASTER7>

Content-Type: text/plain; charset="us-ascii"

 

Mr. Robinow,

 

 

 

The code is only a small test code, not really meant to do anything. 

 

It's just meant to be used as a simple test to see if I can save data 

 

onto an excel sheet. This is the final part of larger code project. 

 

All it does is open an excel file, append data to it, and try to save 

 

the modified file. Most likely, I may have accidentally, left a line 

 

or two out of the code which is probabaly why it did not work for you.

 

I apologize for that error on my part.

 

 

 

Since win32com documentation is not localized, not easily accessible, 

 

at least by me, coding is a trial and error thing for me. 

 

 

 

FYI: I tried your suggestion and it worked. Now if I can only find the 

 

win32com way of finding the last row and column in a sheet, I'd be a 

 

happy camper :-)

 

 

 

Thanks for the assist

 

Little Guy

 

 

 

-----Original Message-----

From: David Robinow [mailto:drobinow at gmail.com] 

Sent: Tuesday, August 23, 2011 12:59 PM

To: The Little Guy

Cc: python-win32 at python.org

Subject: Re: [python-win32] Opening, Modifying, and Saving an Excel File

from Python?

 

 

 

On Sat, Aug 20, 2011 at 10:55 PM, The Little Guy <the_little_guy at gmx.com>

wrote:

 

> Hi,

 

> I apologize for the lengthy post.

 

 The code you posted does not run at all. I had to rename a few things

 

to get it to do anything at all.

 

 

 

It appears that xlrd is holding a reference to the excel file.  I was

 

able to get it to work by adding:

 

 

 

book.release_resources()

 

 

 

 

 

after the xlrd code.

 

-------------- next part --------------

An HTML attachment was scrubbed...

URL:
<http://mail.python.org/pipermail/python-win32/attachments/20110823/99a79267
/attachment-0001.html>

 

------------------------------

 

Message: 3

Date: Wed, 24 Aug 2011 01:24:05 +0000

From: "Andrew MacIntyre" <Andrew.MacIntyre at acma.gov.au>

To: "python-win32 at python.org" <python-win32 at python.org>

Subject: Re: [python-win32] Opening, Modifying, and Saving an Excel

    File from Python? (Tim Roberts) [SEC=PERSONAL]

Message-ID:

    <A1E4E9D08D14924498E135FB4441EE8D071BF8A6 at act01exmbx01vp.internal.govt>

    

Content-Type: text/plain; charset="us-ascii"

 

The code you posted doesn't look right, but I suspect your code is not
properly cleaning up all references to Excel objects before terminating, and
you will find you still have an Excel process in the background which is
hanging on to the file (hence the sharing violations).

 

I have encountered this with scripts that encounter unhandled exceptions.

 

You must ensure that you release all Excel object references before calling
the Quit method on the Excel object.  Using a function or class to
encapsulate the Excel operations can make this easier.

To make it reliable, comprehensive exception handling will also be required.

 

-------------------------> "These thoughts are mine alone!" <---------

Andrew MacIntyre           Operations Branch

tel:   +61 2 6219 5356     Communications Infrastructure Division

fax:   +61 2 6253 3277     Australian Communications & Media Authority

email: andrew.macintyre at acma.gov.au<mailto:andrew.macintyre at acma.gov.au>
http://www.acma.gov.au/

 

From: python-win32-bounces+andrew.macintyre=acma.gov.au at python.org
[mailto:python-win32-bounces+andrew.macintyre=acma.gov.au at python.org] On
Behalf Of The Little Guy

Sent: Wednesday, 24 August 2011 11:08 AM

To: python-win32 at python.org

Subject: Re: [python-win32] Opening, Modifying, and Saving an Excel File
from Python? (Tim Roberts)

 

 

Mr. Roberts,

 

 

 

I've actually, gone through the, routine of placing quit in multiple places
but still I get errors.

 

Either an unknown process error is generated or another instance of Excel
pops up, with a SaveAs

 

dialog, box, with a random filename as default.  Something has grabbed the
excel file.

 

 

 

NOTICE: This email message is for the sole use of the intended recipient(s) 

 and may contain confidential and privileged information. Any unauthorized 

 review, use, disclosure or distribution is prohibited. If you are not the 

 intended recipient, please contact the sender by reply email and destroy
all 

 copies of the original message.

-------------- next part --------------

An HTML attachment was scrubbed...

URL:
<http://mail.python.org/pipermail/python-win32/attachments/20110824/dd042de2
/attachment.html>

 

------------------------------

 

_______________________________________________

python-win32 mailing list

python-win32 at python.org

http://mail.python.org/mailman/listinfo/python-win32

 

 

End of python-win32 Digest, Vol 101, Issue 24

*********************************************

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20110823/a3ff3595/attachment-0001.html>


More information about the python-win32 mailing list