pywin32:将文件另存为xlsx

时间:2017-06-07 16:39:05

标签: python excel pywin32

我尝试使用pywin32将xls文件转换为xlsx文件。这就是我的方式:

from win32com.client import Dispatch

xlApp = Dispatch('Excel.Application')
xlApp.Visible = 1
xlApp.Workbooks.Open('old_file.xls') # An xls file.
xlApp.ActiveWorkbook.SaveAs('new_file.xlsx', FileFormat=50, ConflictResolution=2) # 50=xlsx, 2=overwrite
xlApp.ActiveWorkbook.Close()
xlApp.Quit()

我从here获得了50的值。但是,当我尝试运行它时,我收到此错误:

Traceback (most recent call last):
  File "xlapp_test.py", line 10, in <module>
xlApp.ActiveWorkbook.SaveAs('new_file.xlsx', FileFormat=50, ConflictResolution=2) # 50=xlsx, 2=overwrite
  File "<COMObject <unknown>>", line 7, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type.', u'xlmain11.chm', 0, -2146827284), None)

所以我猜50不起作用。如何使用pywin32将文件另存为xlsx?

0 个答案:

没有答案