Broken examples

John Machin sjmachin at lexicon.net
Tue Aug 12 03:04:15 CEST 2008


On Aug 12, 10:36 am, norseman <norse... at hughes.net> 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.Application' )
> excel.Visible=1                # shows the spreadsheet (can be handy)
> xlCSV = 6
>                                 # substitute for list generation
> list = ["c:\\temp\\test.xls", "c:\\temp\\test1.xls"]
>
> for nm in list:
>    csv = os.path.splitext( 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.
> Documentation on xlCSV                      came from where?
>        "       on .Workbooks.Open(file.xls)  came from ???
>        "       on .SaveAs( filename, xlCSV ) came from ???
>        "       on .Close()                   came from ???
> Tell me about the lack of the .Exit() and/or .Quit(), please.
> I ran out of time today. Is it possible to send something like an
> <enter> keystroke 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.
>
> Tim - thank you again!  At least this works well enough that a clerk can
> press <enter> until the thing runs out of material. :)  Just press and
> hold the <enter> key until the buffer overflow beeps and go do some
> filing and when the screen 'stops' do it again...  Eliminates typos.
>

Why do you want to save as CSV? Would you be happier with a simple
reliable method to extract information from Excel files without all
that bother? If so, consider the xlrd package (http://www.lexicon.net/
sjmachin/xlrd.htm). If you really do need CSV files, still consider
xlrd in combination with the Python csv module -- remove all that user
involvement, allow the user to use Excel for other purposes while the
job is running (instead of filing), programatically handle exception
conditions (like more than one worksheet in the workbook), run it on
Linus or a Mac if you want to, ...




More information about the Python-list mailing list