Re: Broken examples

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • norseman

    Re: Broken examples

    Tim Roberts wrote:
    norseman wrote:
    >Tim;
    > Finally got a chance to test your snippet. Thank you for it!
    >>
    >I took the liberty of flushing it out a bit.
    >============== ==
    >#!/---
    >#
    >import os
    >import win32com.client
    >>
    >excel = win32com.client .Dispatch( 'Excel.Applicat ion' )
    >excel.Visible= 1 # shows the spreadsheet (can be handy)
    >xlCSV = 6
    > # substitute for list generation
    >list = ["c:\\temp\\test .xls", "c:\\temp\\test 1.xls"]
    >>
    >for nm in list:
    > csv = os.path.splitex t( nm )[0] + '.csv'
    > print "Procesing file:", csv
    > if os.path.exists( csv): # window$ won't auto overwrite
    > os.unlink(csv)
    > wb = excel.Workbooks .Open( nm )
    > wb.SaveAs( csv, xlCSV )
    > print wb.Close() # requires manual verification
    > # eliminating unattended runs
    >>
    >#wb.Exit() & wb.Quit() # generate ERROR(s) - no such thing(s)
    > # leaving Excel open
    >>
    >excel.Visible= 0 # actually closes Excel if no sheet open
    > # verified via task manager
    >>
    >print "Check to see if this worked."
    >>
    ># end of file
    >============== ===
    >The above does in fact work.
    >Using the above I have a few questions.
    >Documentatio n on xlCSV came from where?
    > " on .Workbooks.Open (file.xls) came from ???
    > " on .SaveAs( filename, xlCSV ) came from ???
    > " on .Close() came from ???
    >
    MSDN has rather extensive documentation on the Excel object model,
    although it is somewhat spread out. Search for "Excel object model".
    >
    I cheated on xlCSV. That's the name of the symbol, and I believe it
    will even be available as win32com.client .constants.xlCS V after you
    instantiate the application, but I just searched the web for "xlCSV" and
    hard-coded the 6.
    >
    >
    >Tell me about the lack of the .Exit() and/or .Quit(), please.
    >
    Workbooks don't exit or quit. Workbooks can be closed, but exit is
    something that applies to the application (excel, in your case). You
    should also be able to say
    excel = None
    to get rid of the app.
    >
    >
    >I ran out of time today. Is it possible to send something like an
    ><enterkeystrok e to a Window (<yes><no>) box if it accepts same from
    >actual keyboard? May not work anyway since .Close() hangs (in
    >interactive) awaiting it's answer.
    >
    The Workbook.Close method accepts parameters; you should be able to get
    it to skip the dialog. I believe that wb.Close( False ) should do this.
    >
    ==============
    The wb.Close( False) works as predicted.
    The excel = None leaves Excel open.
    excel.Visible=0 does close it so I'm OK.
    excel.Quit() is probably the better choice.
    (learned via a comment above + Trial & Error)


    For the next person that might have need of this:
    >>>>>>>>>test ed/working final snippet
    #!/---
    #
    import os
    import win32com.client
    excel = win32com.client .Dispatch( 'Excel.Applicat ion' )
    excel.Visible=0 #=1 shows the spreadsheet (can be handy)
    xlCSV = 6
    # substituted for list generation
    list = ["c:\\temp\\test .xls", "c:\\temp\\test 1.xls"]
    #
    for nm in list:
    csv = os.path.splitex t( nm )[0] + '.csv'
    print "Procesing file:", csv
    if os.path.exists( csv): # window$/Win-DOS won't auto overwrite
    os.unlink(csv)
    wb = excel.Workbooks .Open( nm )
    wb.SaveAs( csv, xlCSV )
    wb.Close( False )
    #
    excel.Quit() # doing it right
    #
    print "Do a dir *.csv check to see if this worked."
    # end of file
    >>>>>>>>>>>>>
    Using the Python supplied CSV module now makes handling a variety of
    ..xls files a real snap. "Variety" of real spread sheet types, yes. The
    butchered type that should have been done in Word - probably never.

    Thanks again!

    Steve
    norseman@hughes .net
  • John Machin

    #2
    Re: Broken examples

    On Aug 13, 3:48 am, norseman <norse...@hughe s.netwrote:
    Using the Python supplied CSV module now makes handling a variety of
    .xls files a real snap. "Variety" of real spread sheet types, yes. The
    butchered type that should have been done in Word - probably never.
    Please explain how the *CSV* module makes handling any *XLS* files a
    "real snap".

    Comment

    Working...