[英]Python and Excel: Overwriting an existing file always prompts, despite XlSaveConflictResolution value
I'm using the Excel.Application COM object from a Python program to open a CSV file and save it as an Excel workbook. 我正在使用Python程序中的Excel.Application COM对象打开CSV文件并将其另存为Excel工作簿。 If the target file already exists, then I am prompted with this message: "A file named '...' already exists in this location. Do you want to replace it?" 如果目标文件已经存在,那么系统会显示以下消息:“此位置已存在名为'...'的文件。是否要替换它?” That message comes up despite the fact that I have set the XlSaveConflictResolution value to xlLocalSessionChanges, which is supposed to automatically overwrite the changes without prompting -- or so I thought. 尽管事实上我已经将XlSaveConflictResolution值设置为xlLocalSessionChanges这个消息,但它应该会自动覆盖更改而不会提示 - 或者我认为。
I'm using Microsoft Office Excel 2007 (12.0.6535.5002) SP2 MSO and ActivePython 2.6.5.14. 我正在使用Microsoft Office Excel 2007(12.0.6535.5002)SP2 MSO和ActivePython 2.6.5.14。 I have tried all three of the XlSaveConflictResolution values using both constants and integers. 我已经使用常量和整数尝试了所有三个XlSaveConflictResolution值。 I have not tried different versions of Excel. 我没有尝试过不同版本的Excel。
Here's a code snippet: 这是一段代码片段:
import win32com.client
xl = win32com.client.gencache.EnsureDispatch("Excel.Application")
wb = xl.Workbooks.Open(r"C:\somefile.csv")
wb.SaveAs(r"C:\somefile.xls", win32com.client.constants.xlWorkbookNormal, \
None, None, False, False, win32com.client.constants.xlNoChange, \
win32com.client.constants.xlLocalSessionChanges)
And here's the spec from Microsoft about the SaveAs method for an Excel workbook object: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(VS.80).aspx 以下是Microsoft关于Excel工作簿对象的SaveAs方法的规范: http : //msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(VS.80).aspx
Could this be a new "feature" in Excel 2007, or did I just do something wrong? 这可能是Excel 2007中的新“功能”,还是我做错了什么?
Before saving the file set DisplayAlerts
to False
to suppress the warning dialog: 在保存文件之前,将DisplayAlerts
设置为False
以禁止显示警告对话框:
xl.DisplayAlerts = False
After the file is saved it is usually a good idea to set DisplayAlerts
back to True
: 保存文件后,通常最好将DisplayAlerts
设置为True
:
xl.DisplayAlerts = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.