Python and Excel: Overwriting an existing file always prompts, despite XlSaveConflictResolution value(Python 和 Excel:覆盖现有文件总是提示,尽管 XlSaveConflictResolution 值)
问题描述
我正在使用 Python 程序中的 Excel.Application COM 对象打开 CSV 文件并将其保存为 Excel 工作簿.如果目标文件已存在,则会提示我以下消息:此位置已存在名为 '...' 的文件.您要替换它吗?"尽管我已经设置了 XlSaveConflictResolution 值到 xlLocalSessionChanges,它应该会自动覆盖更改而不提示 - 或者我是这么认为的.
I'm using the Excel.Application COM object from a Python program to open a CSV file and save it as an Excel workbook. 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.
我使用的是 Microsoft Office Excel 2007 (12.0.6535.5002) SP2 MSO 和 ActivePython 2.6.5.14.我已经使用常量和整数尝试了所有三个 XlSaveConflictResolution 值.我没有尝试过不同版本的 Excel.
I'm using Microsoft Office Excel 2007 (12.0.6535.5002) SP2 MSO and ActivePython 2.6.5.14. I have tried all three of the XlSaveConflictResolution values using both constants and integers. I have not tried different versions of Excel.
这是一个代码片段:
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)
以下是 Microsoft 关于 Excel 工作簿对象的 SaveAs 方法的规范:http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(VS.80).aspx
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
这可能是 Excel 2007 中的一个新功能",还是我做错了什么?
Could this be a new "feature" in Excel 2007, or did I just do something wrong?
推荐答案
在保存文件前设置 DisplayAlerts 为 False 以抑制警告对话框:
Before saving the file set DisplayAlerts to False to suppress the warning dialog:
xl.DisplayAlerts = False
文件保存后,最好将DisplayAlerts设置回True:
After the file is saved it is usually a good idea to set DisplayAlerts back to True:
xl.DisplayAlerts = True
这篇关于Python 和 Excel:覆盖现有文件总是提示,尽管 XlSaveConflictResolution 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python 和 Excel:覆盖现有文件总是提示,尽管 XlSaveConflictResolution 值
基础教程推荐
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
