0

I am trying to use the Excel 2013 com interface to use the SaveAs function. Everything is working pretty well except that every SaveAs operation will overwrite the existing file.

Related Questions:

Python and Excel: Overwriting an existing file always prompts, despite XlSaveConflictResolution value

Why can't I “save as” an Excel file from my Python code?

This is how I instantiate Excel:

import win32com.client
excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
excel.Visible = 1
wb = excel.Workbooks.Open('D:\\somefileXYZ.xls')
excel.DisplayAlerts = False

The file 'D:\somefile.xls' already exists.

Using the Microsoft reference it should work like this:

wb.SaveAs('D:\\test.xls', win32com.client.constants.xlWorkbookNormal, \
                            None, None, False, False, win32com.client.constants.xlNoChange, \
                            win32com.client.constants.xlOtherSessionChanges)

but it just overwrites the existing file.

Using this it should throw an exception, but this also just overwrites the existing file.

wb.SaveAs('D:\\test.xls')

If I remove this line excel.DisplayAlerts = False Excel asked me if I want to overwrite the existing file. But I want to do it automatically.

I did the same at another computer with Excel 2007 and everything was working fine.

Community
  • 1
  • 1
Philipp
  • 1
  • 1
  • 1
    This may not be what you desire, but you could just use a `os.path.isfile(input_file_path)` before you save to see if the file already exists. – ubiquibacon Oct 07 '15 at 12:30
  • I could do that but I want to save the file if the workbook is the given file and nothing is overwritten. – Philipp Oct 07 '15 at 12:42

0 Answers0