简体   繁体   English

Python和Excel:尽管有XlSaveConflictResolution值,但覆盖现有文件总是会提示

[英]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.

相关问题 Python:覆盖 Excel 文件中的现有工作表 - Python: overwriting an existing sheet in Excel file 图形覆盖到Python中的现有excel文件时丢失 - Graphs lost while overwriting to existing excel file in Python 在不覆盖数据的情况下向现有 excel 文件添加新行(Python) - Add new row to existing excel file without overwriting the data (Python) 使用 Python 和来自 Excel 的现有数据生成饼图,而不覆盖 Excel 文件 - Generate Pie Chart with Python with already existing data from Excel without overwriting the Excel file 使用Python覆盖XLSX文件中的现有单元格 - Overwriting existing cells in an XLSX file using Python 读取Excel中的单元格值并使用python写入现有的Excel文件 - Reading a cell value in Excel and write into an othet existing Excel file with python 熊猫-将Excel文件保存回/覆盖现有工作表 - Pandas - Saving Excel File back into/overwriting an existing sheet 如何在不覆盖数据的情况下写入现有的 excel 文件(使用熊猫)? - How to write to an existing excel file without overwriting data (using pandas)? 如何在不覆盖现有文件的情况下在 python 中创建文件 - How do I create a file in python without overwriting an existing file 使用 ElementTree 和 Python 覆盖 XML 文件时保留现有命名空间 - Keep Existing Namespaces when overwriting XML file with ElementTree and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM