Excel process still running after program completion.

Discussion in 'Python' started by Chris, Sep 11, 2007.

  1. Chris

    Chris Guest

    I have a python script that is driving Excel and using the win32com
    module. However, upon program completion there's still an Excel.exe
    process running in the background that I must terminate through Task
    Manager. Reading up on other threads indicate that maybe I still have
    some Excel objects referenced within my code. Is this why the process
    doesn't terminate?

    The related (I hope) parts of my code is here.

    x1App = Dispatch("Excel.Application")
    Book1 = x1App.Workbooks.Open(ExcelLogPath+"\\outputLog-template.xls")
    x1App.Visible = 1
    for sheets in Book1.Worksheets:
    if sheets.Name == file_name:
    sheetExists = True
    if sheetExists != True:
    activeSheet =
    Book1.Worksheets.Add(After=Book1.Worksheets(1))
    activeSheet.Name = file_name
    testNum[file_name] = 0
    Book1.Worksheets(file_name).Select()
    Book1.ActiveSheet.Cells(1+(testNum[file_name]*20),1).Value = "Original
    File Name"
    Book1.ActiveSheet.Cells(2+(testNum[file_name]*20),1).Value =
    file_name
    Book1.ActiveSheet.Pictures().Insert(output).Select()
    Book1.SaveAs(Filename=path)
    x1App.ActiveWorkbook.Close(SaveChanges=0)
    x1App.Quit()
    del x1App
    del Book1
    del activeSheet

    What am I missing?
    Chris, Sep 11, 2007
    #1
    1. Advertising

  2. Chris

    Chris Guest

    On Sep 11, 12:59 pm, "Hamilton, William " <> wrote:
    > > From: Chris

    >
    > > I have a python script that is driving Excel and using the win32com
    > > module. However, upon program completion there's still an Excel.exe
    > > process running in the background that I must terminate through Task
    > > Manager. Reading up on other threads indicate that maybe I still have
    > > some Excel objects referenced within my code. Is this why the process
    > > doesn't terminate?

    >
    > > The related (I hope) parts of my code is here.

    >
    > > x1App = Dispatch("Excel.Application")
    > > Book1 = x1App.Workbooks.Open(ExcelLogPath+"\\outputLog-template.xls")
    > > x1App.Visible = 1
    > > for sheets in Book1.Worksheets:
    > > if sheets.Name == file_name:
    > > sheetExists = True
    > > if sheetExists != True:
    > > activeSheet =
    > > Book1.Worksheets.Add(After=Book1.Worksheets(1))
    > > activeSheet.Name = file_name
    > > testNum[file_name] = 0
    > > Book1.Worksheets(file_name).Select()
    > > Book1.ActiveSheet.Cells(1+(testNum[file_name]*20),1).Value = "Original
    > > File Name"
    > > Book1.ActiveSheet.Cells(2+(testNum[file_name]*20),1).Value =
    > > file_name
    > > Book1.ActiveSheet.Pictures().Insert(output).Select()
    > > Book1.SaveAs(Filename=path)
    > > x1App.ActiveWorkbook.Close(SaveChanges=0)
    > > x1App.Quit()
    > > del x1App
    > > del Book1
    > > del activeSheet

    >
    > > What am I missing?

    >
    > In my Excel projects, I terminate it with:
    >
    > xlBook.Close()
    > xlApp.Quit()
    >
    > I haven't had a problem with Excel staying open after the program ends.
    >
    > (On a tangent, I want to find the person who thought it was a good idea
    > to use the same symbol in a font for 1, l, and I and do some unpleasant
    > things.)
    >
    > --
    > -Bill Hamilton


    That doesn't really fix the problem as I'm pretty sure its identical
    code for
    x1App.ActiveWorkbook.Close(SaveChanges=0)

    I think where my problem lies is within my for each loop where I
    search to see if the worksheet with the name already exists
    for sheets in Book1.Worksheets:
    if sheets.Name == file_name:
    sheetExists = True


    Since I'm assuming that it creates objects for each of the sheets and
    I don't delete them...
    Chris, Sep 11, 2007
    #2
    1. Advertising

  3. Chris

    Chris Guest

    On Sep 11, 1:26 pm, Chris <> wrote:
    > On Sep 11, 12:59 pm, "Hamilton, William " <> wrote:
    >
    >
    >
    > > > From: Chris

    >
    > > > I have a python script that is driving Excel and using the win32com
    > > > module. However, upon program completion there's still an Excel.exe
    > > > process running in the background that I must terminate through Task
    > > > Manager. Reading up on other threads indicate that maybe I still have
    > > > some Excel objects referenced within my code. Is this why the process
    > > > doesn't terminate?

    >
    > > > The related (I hope) parts of my code is here.

    >
    > > > x1App = Dispatch("Excel.Application")
    > > > Book1 = x1App.Workbooks.Open(ExcelLogPath+"\\outputLog-template.xls")
    > > > x1App.Visible = 1
    > > > for sheets in Book1.Worksheets:
    > > > if sheets.Name == file_name:
    > > > sheetExists = True
    > > > if sheetExists != True:
    > > > activeSheet =
    > > > Book1.Worksheets.Add(After=Book1.Worksheets(1))
    > > > activeSheet.Name = file_name
    > > > testNum[file_name] = 0
    > > > Book1.Worksheets(file_name).Select()
    > > > Book1.ActiveSheet.Cells(1+(testNum[file_name]*20),1).Value = "Original
    > > > File Name"
    > > > Book1.ActiveSheet.Cells(2+(testNum[file_name]*20),1).Value =
    > > > file_name
    > > > Book1.ActiveSheet.Pictures().Insert(output).Select()
    > > > Book1.SaveAs(Filename=path)
    > > > x1App.ActiveWorkbook.Close(SaveChanges=0)
    > > > x1App.Quit()
    > > > del x1App
    > > > del Book1
    > > > del activeSheet

    >
    > > > What am I missing?

    >
    > > In my Excel projects, I terminate it with:

    >
    > > xlBook.Close()
    > > xlApp.Quit()

    >
    > > I haven't had a problem with Excel staying open after the program ends.

    >
    > > (On a tangent, I want to find the person who thought it was a good idea
    > > to use the same symbol in a font for 1, l, and I and do some unpleasant
    > > things.)

    >
    > > --
    > > -Bill Hamilton

    >
    > That doesn't really fix the problem as I'm pretty sure its identical
    > code for
    > x1App.ActiveWorkbook.Close(SaveChanges=0)
    >
    > I think where my problem lies is within my for each loop where I
    > search to see if the worksheet with the name already exists
    > for sheets in Book1.Worksheets:
    > if sheets.Name == file_name:
    > sheetExists = True
    >
    > Since I'm assuming that it creates objects for each of the sheets and
    > I don't delete them...


    Which I'm right... I added this code and now there's no EXCEL.EXE
    process after my script completes

    for sheets in Book1.Worksheets:
    if sheets.Name == file_name:
    sheetExists = True
    sheets = None
    del sheets
    Chris, Sep 11, 2007
    #3
  4. Chris

    Tal Einat Guest

    Re: Excel process still running after program completion - SOLVED

    On Sep 11, 8:29 pm, Chris <> wrote:
    > On Sep 11, 1:26 pm, Chris <> wrote:
    >
    >
    >
    > > On Sep 11, 12:59 pm, "Hamilton, William " <> wrote:

    >
    > > > > From: Chris

    >
    > > > > I have a python script that is driving Excel and using the win32com
    > > > > module. However, upon program completion there's still an Excel.exe
    > > > > process running in the background that I must terminate through Task
    > > > > Manager. Reading up on other threads indicate that maybe I still have
    > > > > some Excel objects referenced within my code. Is this why the process
    > > > > doesn't terminate?

    >
    > > > > The related (I hope) parts of my code is here.

    >
    > > > > x1App = Dispatch("Excel.Application")
    > > > > Book1 = x1App.Workbooks.Open(ExcelLogPath+"\\outputLog-template.xls")
    > > > > x1App.Visible = 1
    > > > > for sheets in Book1.Worksheets:
    > > > > if sheets.Name == file_name:
    > > > > sheetExists = True
    > > > > if sheetExists != True:
    > > > > activeSheet =
    > > > > Book1.Worksheets.Add(After=Book1.Worksheets(1))
    > > > > activeSheet.Name = file_name
    > > > > testNum[file_name] = 0
    > > > > Book1.Worksheets(file_name).Select()
    > > > > Book1.ActiveSheet.Cells(1+(testNum[file_name]*20),1).Value = "Original
    > > > > File Name"
    > > > > Book1.ActiveSheet.Cells(2+(testNum[file_name]*20),1).Value =
    > > > > file_name
    > > > > Book1.ActiveSheet.Pictures().Insert(output).Select()
    > > > > Book1.SaveAs(Filename=path)
    > > > > x1App.ActiveWorkbook.Close(SaveChanges=0)
    > > > > x1App.Quit()
    > > > > del x1App
    > > > > del Book1
    > > > > del activeSheet

    >
    > > > > What am I missing?

    >
    > > > In my Excel projects, I terminate it with:

    >
    > > > xlBook.Close()
    > > > xlApp.Quit()

    >
    > > > I haven't had a problem with Excel staying open after the program ends.

    >
    > > > (On a tangent, I want to find the person who thought it was a good idea
    > > > to use the same symbol in a font for 1, l, and I and do some unpleasant
    > > > things.)

    >
    > > > --
    > > > -Bill Hamilton

    >
    > > That doesn't really fix the problem as I'm pretty sure its identical
    > > code for
    > > x1App.ActiveWorkbook.Close(SaveChanges=0)

    >
    > > I think where my problem lies is within my for each loop where I
    > > search to see if the worksheet with the name already exists
    > > for sheets in Book1.Worksheets:
    > > if sheets.Name == file_name:
    > > sheetExists = True

    >
    > > Since I'm assuming that it creates objects for each of the sheets and
    > > I don't delete them...

    >
    > Which I'm right... I added this code and now there's no EXCEL.EXE
    > process after my script completes
    >
    > for sheets in Book1.Worksheets:
    > if sheets.Name == file_name:
    > sheetExists = True
    > sheets = None
    > del sheets


    You just need to 'del sheets' once after the loop, no need to do it in
    every iteration. After your original loop the 'sheets' variable still
    pointed to the last sheet object, probably causing it not to be
    properly cleaned up. This is the only thing that your change fixes,
    but it is simpler (and more readable) to just do it once after the
    loop.

    (Please add SOLVED or FIXED to the subject when your issue has been
    resolved, so that people like me don't read the whole thread only to
    find that their help is no longer needed.)

    - Tal
    Tal Einat, Sep 12, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    1
    Views:
    338
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 5, 2005
  2. Claudiu Nicolaie CISMARU
    Replies:
    5
    Views:
    896
    Claudiu Nicolaie CISMARU
    May 25, 2011
  3. ziggs
    Replies:
    2
    Views:
    172
    Henry Robinson
    Apr 21, 2004
  4. Unnsse Khan
    Replies:
    5
    Views:
    125
    Peter Burns
    Oct 22, 2005
  5. Olivier GROSSET-GRANGE

    JNI, threads are still running after DestroyJavaVM()

    Olivier GROSSET-GRANGE, Feb 19, 2013, in forum: Java
    Replies:
    2
    Views:
    830
Loading...

Share This Page