win32com: Trouble using Excel's "SaveAs" method for Workbooks and Worksheet objects

Ralf Laukien lau at adtranzsig.de
Wed Oct 4 06:53:36 EDT 2000


I had the same problem until I found out that this works:

    xl.Workbooks.Open(sourcefile)  
    xl.ActiveWorkbook.SaveAs(Filename=destfile, FileFormat="6") # 6 =
csv
    xl.Workbooks(1).Close(0) 

Ralf


"S. Murthy Kambhampaty" schrieb:
> 
> I am trying to program Python to make Excel write comma separated variable
> (CSV) files.  I'm having trouble using Excel's "SaveAs" method for Workbook
> and Worksheet objects from Python with the win32comm extensions.  Wondering
> if anybody's had any success using the SaveAs method for Worksheets and
> Workbooks provided by COM.
> 
> I have:
> 
> 1. Followed directions for building excel8.py from excel8.olb
> 2. Created a Workbook object with a single sheet in it called xlBook2
> (creation script shown below)
> 3. Issuing xlBook2.SaveAs(Filename=r'd:\temp\test01.csv',
> FileFormat='xlCSVWindows') produces the
> following error message:
> "Traceback (innermost last):
>   File "<interactive input>", line 1, in ?
>   File
> "d:\Python16\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x2.py",
> line 17017, in SaveAs
> com_error: (-2147352567, 'Exception occurred.', (1004, 'Microsoft Excel',
> 'SaveAs method of Workbook class failed', 'XLMAIN8.HLP', 0, 0), None)"
> 
> Any help/suggestions would be appreciated.
> 
> Thanks, smk
> 
> Python script for creating xlBook2 follows:
> # Begin python script
> import excel8, os
> 
> os.chdir(r'C:\Temp')
> 
> xlApp = excel8.Application()
> xlApp.Workbooks.Open(r'C:\Temp\Test1002.XLS')
> # xlBook = xlApp.Workbooks(1)
> # xlSheet = xlApp.Workbooks(1).Worksheets('Page (6)')
> xlApp.Workbooks('Test1002.XLS').Worksheets('Page (6)').Activate()
> xlSheet.Copy()
> xlBook2 = xlApp.Workbooks(2)
> # end Python script



More information about the Python-list mailing list