0
当我现在尝试保存一个XLSM文件,像这样:Pywin32保存为XLSM文件,而不是XLSX
from win32com.client import Dispatch
# Open Excel workbook
xl = Dispatch("Excel.Application")
wb = xl.Workbooks.Add(r"C:\Users\ryan\Desktop\Book1.xlsm")
# Make some changes
# blah blah blah
# Save the workbook in XLSM format with new name
wb.SaveAs(r"C:\Users\ryan\Desktop\Book1 - XLSM.xlsm")
xl.Quit()
我提示以下错误......
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<COMObject Add>", line 7, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel'
, 'This extension can not be used with the selected file type. Change the file e
xtension in the File name text box or select a different file type by changing t
he Save as type.', 'xlmain11.chm', 0, -2146827284), None)
如何保存作为一种新的文件格式?
注意:当尝试将已经是XLSM的文件保存为XLSM时,会导致相同的错误。
感谢促成这一点。是的,我在发布问题后不久就想到了这一点,但我只是使用数字52作为文件格式,而不是键入整个“xlOpenXMLWorkbookMacroEnabled”。 – ryansin
你也可以像win32com一样使用win32com的枚举:''win32com.client.constants.xlOpenXMLWorkbookMacroEnabled'' – chip